SpringMVC中Controller如何获取Form表单提交的数据
表单提交的数据也就是请求数据,分为Get和Post两种⽅式提交。
Controller中有三种⽅式获取表单数据:
Controller的⽅法,添加HttpServletRequst类型⼊参,通过Parameter()获取请求数据
Controller的⽅法,添加对应表单字段name的参数,有⼏个表单字段就添加多少个对应的⼊参,如下
@RequestMapping(value="/user/save", method=RequestMethod.POST)
private String doSave(@RequestParam("userName") String userName, @RequestParam("age") Integer age, HttpSession session){
Controller的⽅法,添加⾃定义Java类型的⼊参,并添加@ModelAttribute注解(实际上,可以不添加@ModelAttribute注解) ,由这个⼊参对象接收表单提交的数据,如下
@RequestMapping(value="/user/save", method=RequestMethod.POST)
private String doSave(@ModelAttribute User user, HttpSession session){
从上述描述,可以看出这⼏种⽅式的优缺点。
在这⾥,我推荐使⽤第三种⽅式,添加⾃定义Java类型的⼊参
下⾯我们就看看demo,如何使⽤第三种⽅式实现接收表单提交的数据
处理表单提交的Controller,FormSubmitController.java:
package ller;
import javax.servlet.http.HttpSession;
import org.apachemons.lang.builder.ReflectionToStringBuilder;
import org.apachemons.lang.math.RandomUtils;
import org.apache.log4j.Logger;
import t.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import del.User;
/**
* @编写⼈: yh.zeng
* @编写时间:2017-7-15 下午12:14:41
* @⽂件描述: 表单提交demo
*/
@Controller
@Scope("singleton") //只实例化⼀个bean对象(即每次请求都使⽤同⼀个bean对象),默认是singleton public class FormSubmitController {
private Logger logger = Logger(FormSubmitController.class);
@RequestMapping(value="/user/view/{userId}", method=RequestMethod.GET)
private String viewUser(@PathVariable("userId") String userId){
return "user/view";
}
@RequestMapping(value="/admin/user", method=RequestMethod.GET, params="add")
private String addUser(){
return "user/add";
}
@RequestMapping(value="/user/save", method=RequestMethod.POST)
private String doSave(@ModelAttribute User user, HttpSession session){
user.Int(1000)); //模拟数据库持久化
/**
* 进⾏数据库的持久化,省略
*/
logger.String(user));
session.setAttribute("user", user);
return "redirect:/user/view/"+No();
}
}
添加⽤户页⾯add.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加⽤户</title>
</head>
<body>
<form action="${tPath}/user/save" method="post">
⽤户名:<input type="text" name="userName"/> <br><br>
年龄:<input type="text" name="age"/> <br><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
查看⽤户信息页⾯view.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查看⽤户信息</title>
springmvc的注解有哪些</head>
<body>
⽤户编号: ${} <br>
⽤户名: ${sessionScope.user.userName} <br>
年龄:${sessionScope.user.age}
</body>
</html>
Spring MVC配置的视图解析器:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
效果:
点击【提交】之后,跳转到查看新添加的⽤户信息的页⾯
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论