首页 > 其他分享 >【Spring MVC】简单数据绑定

【Spring MVC】简单数据绑定

时间:2023-04-11 15:45:30浏览次数:26  
标签:RequestMapping Spring 绑定 id MVC 参数 import public String

实体类:

 

 spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置springMVC需要扫描的包 -->
    <context:component-scan base-package="com.xiaobiti.controller"/>
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="WEB-INF/pages"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

 UserController.java

package com.xiaobiti.controller;

import com.xiaobiti.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {
    //方法一
    @RequestMapping("/getId")
    //传入参数为请求对象
    public void getId(HttpServletRequest request){
        String id = request.getParameter("id");
        System.out.println(id);
    }
    //方法二
    @RequestMapping("/getUser")
    //传入参数直接对应get请求中发送的请求参数
    public void getUsernameAndId(String userName,String id){
        System.out.println("userName=" + userName + "," + "id=" + id);
    }
    //方法三
    @RequestMapping("/getUser2")
    //传入参数是将get请求中发送的请求参数封装成一个对象
    public void getUser(User user){
        System.out.println(user);
    }
}

跳转测试页面index.jsp代码:

<html>
<body>
<h2>Hello World!</h2>
 <a href="firstController">Link</a><br>
 <a href="getId?id=My_id">getId</a><br>
 <a href="getUser?userName=abc&id=2">getUser</a><br>
  <a href="getUser2?username=abc&password=123456">getUser2</a><br>
</body>
</html>

 小小实例:

实体类:

 

 

 

 

 ProductController.java

package com.xiaobiti.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class ProductController {
    /**
    * 获得商品列表
    */
//    @RequestMapping("/getPros")
//    //传入参数对应请求参数中的全部proIds的值的一个数组
//    public void getPros(String[] proIds){
//        for (String id:proIds) {
//            System.out.println(id);
//        }
//    }
    @RequestMapping("/getPros")
    //传入参数对应请求参数中的全部proIds的值的一个集合
    public void getPros(@RequestParam("proIds")List<String> list){
        for (String id:list) {
            System.out.println(id);
        }
    }
}

商品列表页面product.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    项目路径:${pageContext.request.contextPath}
    <form action="${pageContext.request.contextPath}/getPros"method="post">
        <table width="220px" border="1">
            <tr><td>选择</td><td>商品名称</td></tr>
            <tr>
                <!-- 这里只展示了一个商品 -->
                <td><input name="proIds" value="1" type="checkbox"></td>
                <td>Java基础</td>
            </tr>
            <tr>
                <!-- 这里只展示了一个商品 -->
                <td><input name="proIds" value="2" type="checkbox"></td>
                <td>JavaWeb</td>
            </tr>
            <tr>
                <!-- 这里只展示了一个商品 -->
                <td><input name="proIds" value="3" type="checkbox"></td>
                <td>SSM框架实战</td>
            </tr>
        </table>
        <input type="submit" value="提交商品"/>
    </form>    
</body>
</html>

 

标签:RequestMapping,Spring,绑定,id,MVC,参数,import,public,String
From: https://www.cnblogs.com/xiaobiti/p/17306201.html

相关文章

  • 自定义SpringBoot Starter
    1.Starter加载原理Springboot通过SpringBootApplication注解启动项目,springboot启动的时候,会将项目中所有声明为bean对象的实例加载到IOC容器。除此之外也会将starter里的bean信息加载到ioc容器,从而做到0配置,开箱即用。1.1加载starter:Springboot项目启动时,Springboot通过@Spri......
  • 【Spring boot】 @Value注解
    一、不通过配置文件的注入属性1.1注入普通字符串直接附在属性名上,在Bean初始化时,会赋初始值@Value("normal")privateStringnormal;1.2注入java系统变量@Value("#{systemProperties['os.name']}")privateStringsystemPropertiesName;//注入操作系统属性1.3注......
  • SpringBoot---文件上传
    静态资源访问使用IDEA创建SPringBoot项目,会默认创建出classpath:/static/目录,静态资源一般放在这个目录下即可。如果默认的静态资源过滤策略不能满足开发需求,也可以自定义静态资源过滤策略。在application.properties中直接定义过滤规则和静态资源位置:spring.mvc.stati......
  • Springboot报错:Could not resolve view with name 'index' in servlet with name 'dis
    该异常是因为用定义了带@EnableWebMvc注解的配置类后发生的,在带该注解的配置类中加入下面的代码就可以了:@BeanpublicInternalResourceViewResolverviewResolver(){InternalResourceViewResolverviewResolver=newInternalResourceViewResolver();viewResolver.......
  • SpringBoot线程池和Java线程池的实现原理
    使用默认的线程池方式一:通过@Async注解调用publicclassAsyncTest{@Asyncpublicvoidasync(Stringname)throwsInterruptedException{System.out.println("async"+name+""+Thread.currentThread().getName());Thread.sleep(10......
  • SpringBoot 集成 MybatisPlus 五——ActiveRecord
    1什么是ActiveRecordActiveRecord(活动记录),是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录。在ActiveRecord模式中,对象中既有持久存储的数据,也有针对数据的操作,ActiveRecord模式把数据增删改查的逻辑作为对象的一......
  • SpringSecurity源码-构建ProviderManager
    简介在构建WenSecurity执行生命周期AbstractConfiguredSecurityBuilder#doBuild()方法中的init(),会执行到WebSecurityConfigurerAdapter#init(WebSecurityweb)方法,会去创建HttpSecurity。在创建HttpSecurity时调用authenticationManager()构建ProviderManager。 WebSecurityCo......
  • ASP.NET Core MVC 从入门到精通之接化发(二)
    随着技术的发展,ASP.NETCoreMVC也推出了好长时间,经过不断的版本更新迭代,已经越来越完善,本系列文章主要讲解ASP.NETCoreMVC开发B/S系统过程中所涉及到的相关内容,适用于初学者,在校毕业生,或其他想从事ASP.NETCoreMVC系统开发的人员。 经过前两篇文章的讲解,初步了解ASP.NETCor......
  • spring boot单库动态分表实现【增删查】(含源码)
    一.背景现实场景中当个别业务数据量过大时会影响系统功能性能,当整个业务还没有达到分库的级别时,动态分表也是一个的选择,基本思想是按照一定维度将数据分表存储动态查询。本次实现的是基于springboot的单表动态增删查,首先分表的规则根据一个格式生产,包含时间在其中,每一条数据......
  • day06-SpringCloud Ribbon
    SpringCloudRibbon1.Ribbon介绍1.1Ribbon是什么?官网地址:Netflix/ribbon:Ribbon(github.com)SpringCloudRibbon是基于NetflixRibbon实现的一套客户端负载均衡的工具Ribbon主要功能是提供客户端负载均衡算法和服务调用Ribbon客户端组件提供一系列完整的配置项如连......