java注解如何获得表单的内容

本文介绍SpringMVC中处理表单提交数据的三种方法,重点推荐并演示了使用自定义Java类型参数配合@ModelAttribute注解的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来源:https://blog.youkuaiyun.com/yh_zeng2/article/details/75172990

表单提交的数据也就是请求数据,分为Get和Post两种方式提交。

Controller中有三种方式获取表单数据:

  •  Controller的方法,添加HttpServletRequst类型入参,通过HttpServletRequst.getParameter()获取请求数据
  •  Controller的方法,添加对应表单字段name的参数,有几个表单字段就添加多少个对应的入参,如下          
[java]  view plain  copy
  1. @RequestMapping(value="/user/save", method=RequestMethod.POST)  
  2. private String doSave(@RequestParam("userName") String userName, @RequestParam("age") Integer age, HttpSession session){  
  •  Controller的方法,添加自定义Java类型的入参,并添加@ModelAttribute注解(实际上,可以不添加@ModelAttribute注解) ,由这个入参对象接收表单提交的数据,如下
[java]  view plain  copy
  1. @RequestMapping(value="/user/save", method=RequestMethod.POST)  
  2. private String doSave(@ModelAttribute User user, HttpSession session){  


从上述描述,可以看出这几种方式的优缺点。

在这里,我推荐使用第三种方式,添加自定义Java类型的入参

下面我们就看看demo,如何使用第三种方式实现接收表单提交的数据

处理表单提交的Controller,FormSubmitController.java:

[java]  view plain  copy
  1. package edu.mvcdemo.controller;  
  2.   
  3. import javax.servlet.http.HttpSession;  
  4. import org.apache.commons.lang.builder.ReflectionToStringBuilder;  
  5. import org.apache.commons.lang.math.RandomUtils;  
  6. import org.apache.log4j.Logger;  
  7. import org.springframework.context.annotation.Scope;  
  8. import org.springframework.stereotype.Controller;  
  9. import org.springframework.web.bind.annotation.ModelAttribute;  
  10. import org.springframework.web.bind.annotation.PathVariable;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.bind.annotation.RequestMethod;  
  13. import edu.mvcdemo.model.User;  
  14.   
  15. /** 
  16.  * @编写人: yh.zeng 
  17.  * @编写时间:2017-7-15 下午12:14:41 
  18.  * @文件描述: 表单提交demo 
  19.  */  
  20. @Controller  
  21. @Scope("singleton"//只实例化一个bean对象(即每次请求都使用同一个bean对象),默认是singleton  
  22. public class FormSubmitController {  
  23.       
  24.     private Logger logger = Logger.getLogger(FormSubmitController.class);  
  25.       
  26.     @RequestMapping(value="/user/view/{userId}", method=RequestMethod.GET)  
  27.     private String viewUser(@PathVariable("userId") String userId){  
  28.         return "user/view";  
  29.     }  
  30.       
  31.     @RequestMapping(value="/admin/user", method=RequestMethod.GET, params="add")  
  32.     private String addUser(){  
  33.         return "user/add";  
  34.     }  
  35.       
  36.     @RequestMapping(value="/user/save", method=RequestMethod.POST)  
  37.     private String doSave(@ModelAttribute User user, HttpSession session){  
  38.   
  39.           
  40.         user.setNo(RandomUtils.nextInt(1000)); //模拟数据库持久化  
  41.           
  42.         /** 
  43.          * 进行数据库的持久化,省略 
  44.          */  
  45.   
  46.         logger.info(ReflectionToStringBuilder.toString(user));  
  47.           
  48.         session.setAttribute("user", user);  
  49.           
  50.         return "redirect:/user/view/"+user.getNo();  
  51.     }  
  52. }  

添加用户页面add.jsp:

[html]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>添加用户</title>  
  8. </head>  
  9. <body>  
  10.    <form action="${pageContext.request.contextPath}/user/save" method="post">  
  11.               用户名:<input type="text" name="userName"/> <br><br>  
  12.               年龄:<input type="text" name="age"/> <br><br>  
  13.        <input type="submit" value="提交"/>  
  14.    </form>  
  15. </body>  
  16. </html>  

查看用户信息页面view.jsp:

[html]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>查看用户信息</title>  
  8. </head>  
  9. <body>  
  10.               用户编号: ${sessionScope.user.no} <br>  
  11.               用户名: ${sessionScope.user.userName} <br>  
  12.               年龄:${sessionScope.user.age}   
  13. </body>  
  14. </html>  

Spring MVC配置的视图解析器:

[html]  view plain  copy
  1. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  2.     <property name="prefix" value="/jsp/" />  
  3.     <property name="suffix" value=".jsp" />  
  4. </bean>  


效果:

点击【提交】之后,跳转到查看新添加的用户信息的页面




 项目demo见https://github.com/zengyh/MavenSpringMvcDemo.git

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值