為何我用spring mvc獲取不到表單提交資料?

傻了吧唧不愣登發表於2018-01-31

這個問題也困擾了我很久,自己學習用的很多專案也需要表單提交,用@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>

相關文章