Spring MVC中 视图 向 控制器 传参(接收方式)
2020-11-25 本文已影响0人
凡哥爱丽姐
1、方法一(通过HttpServletRequest方式接收)
1.1、添加servlet的jar包
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
1.2、spring.xml以及web.xml配置如上一章所示
1.3、创建index.jsp和success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<a href="/user?username=weifan&password=123">test</a>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Mr Wei
Date: 2020/*/*
Time: 15:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>测试成功!</h1>
</body>
</html>
1.4、添加Controller类
package com.fan.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class Controller1 {
@RequestMapping("/user")
public String test2(HttpServletRequest request){
String u = request.getParameter("username");
String p = request.getParameter("password");
System.out.println(u+" "+p);
return "success";
}
}
1.5、启动tomcat进行测试
2、方法二(页面传值时的key=处理请求的方法的参数名)
2.1、jar包的添加,spring.xml以及web.xml配置如上一章所示
2.2、创建index.jsp和success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<a href="/user?username=weifan">test</a>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Mr Wei
Date: 2020/*/*
Time: 15:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>测试成功!</h1>
</body>
</html>
2.3、添加Controller类
package com.fan.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class Controller1 {
@RequestMapping("/user")
//传入的值 即username要和前端传入的key值相同。页面传值时的key=处理请求的方法的参数名
public String test1(String username){
System.out.println("传入的用户信息是:"+username);
return "success";
}
}
2.4、启动tomcat进行测试(2种测试方式)
2.4.1、我们可以在地址栏直接输入http://localhost:8080/user?username=weifan进行访问。
2.4.2、点击index.jsp页面的test链接,也可以实现相应的跳转。
2种测试方式控制台结果如下如所示
测试结果3、方法三(封装成对象,使用控件名和对象的属性名一致的方式进行接收)
3.1、jar包的添加,spring.xml以及web.xml配置如上一章所示
3.2、创建index.jsp和success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<form method="post" action="/user">
<input type="text" name="username"/><br/>
<input type="text" name="password"><br/>
<input type="checkbox" name="box" value="1">1
<input type="checkbox" name="box" value="2">2
<input type="checkbox" name="box" value="3">3
<input type="checkbox" name="box" value="4">4
<input type="checkbox" name="box" value="5">5
<input type="submit" value="提交"/>
</form>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Mr Wei
Date: 2020/*/*
Time: 15:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>测试成功!</h1>
</body>
</html>
3.3、添加实体类
public class User {//使用控件名和对象的属性名一致的方式进行接收,即username和前端的表单中的name值一致
private String username;
private String password;
private String[] box; //list集合接收也可以
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String[] getBox() {
return box;
}
public void setBox(String[] box) {
this.box = box;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", box=" + Arrays.toString(box) +
'}';
}
}
3.4、添加Controller类
@Controller
public class TestController {
//(3)使用控件名和对象的属性名一致的方式进行接收
@RequestMapping("/user")
public String test(User user){
System.out.println(user);
return "success";
}
}
3.5、启动tomcat进行测试
测试结果4、随笔测试
4.1、index.jsp和success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<form action="/user" method="post">
<input type="text" name="username"><br/>
<input type="text" name="password"><br/>
<input type="text" name="day"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Mr Wei
Date: 2020/*/*
Time: 15:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>测试成功!</h1>
</body>
</html>
4.2、Controller类
package com.fan.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Date;
@Controller
public class Controller1 {
@RequestMapping("/user")
public String test3(String username, String password, Date day){
System.out.println(username+"--"+password+"--"+day);
return "success";
}
// @InitBinder
// public void initBinder(WebDataBinder binder, WebRequest request) {
// //转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
// DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
// binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
// }
}
4.3、启动tomcat进行测试
图片1.png若输入的日期格式如:yyyy-MM-dd,则会报错。
图片2.png
若输入的日期格式如:yyyy/MM/dd,则会跳转成功,Spring MVC框架默认支持转换的日期格式是yyyy/MM/dd。
解决日期问题方式:
(1)使用string接受日期,接受后,再转换: SimpleDataFormate
(2)使用工具类处理日期(见有道云笔记SpringIOC1)