这个问题也困扰了我很久,自己学习用的很多项目也需要表单提交,用@ModelAttribute和@RequestParam都没有用。。
不知道是否是配置有问题,以下是部分代码,使用的IDE是idea15.0,
第一次用CSDN问问题,可能格式不规范,多多包涵。
Controller:
@Controller public class UserController { private static final Log logger = LogFactory.getLog(UserController.class); @RequestMapping(value = "/{formName}") public String loginForm( @PathVariable String formName){ return formName; } @RequestMapping(value = "/register", method = RequestMethod.POST) public String login( // @RequestParam(value = "loginname", required = false) String loginname, //没用 // @RequestParam(value = "birthday", required = false) Date birthday, //没用 @ModelAttribute("user") User user, //没用 Model model){ logger.info(user); model.addAttribute("user", user); return "success"; } }
jsp:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <title>注册页面</title> </head> <body> <h3>注册页面</h3> <form method="post" action="register"> <table> <tr> <td><label>登录名:</label></td> <td><input type="text" id="loginname" name="loginname"></td> </tr> <tr> <td><label>生日:</label></td> <td><input type="text" id="birthday" name="birthday"></td> </tr> <tr> <td><input id="submit" type="submit" value="注册" /></td> </tr> </table> </form> </body> </html>springmvc-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd"> <!--spring可以自动去扫描base-pack下面的包或子包下面的Java文件,如果扫描到有Spring的相关注解的类,则 把这些类注册为Spring的bean--> <context:component-scan base-package="org.fkit" /> <!--配置annotation类型的处理映射器--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <!--配置annotation类型的处理器适配器--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <!--前缀--> <property name="prefix"> <value>/jsp/</value> </property> <!--后缀--> <property name="suffix"> <value>.jsp</value> </property> </bean> <!--装配自定义的格式化转换器:String转换Date类型用的--> <mvc:annotation-driven conversion-service="conversionService" /> <!--DateFormatter bean--> <bean id="dateFormatter" class="org.fkit.formatter.DateFormatter" p:datePattern="yyyy-MM-dd" /> <!--格式化--> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="formatterRegistrars"> <set> <bean class="org.fkit.formatter.MyFormatterRegisterar" p:dateFormatter-ref="dateFormatter" /> </set> </property> </bean> </beans>web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>