Spring 登录表单不发送参数

Spring login form don't sent parameters

我尝试修改网上找到的登录示例。 注册页面 运行 正确。

我想登录并 return 在索引中记录。 我的代码没有将参数从 LoginController 传递到 indexSuccess,为什么?

注册页面(我没有发布)是相同的,但 运行 并且可以正确通信。

我的代码是这样的。

请知道的人告诉我哪里错了?

indexController.java

package net.codejava.spring.controller;
import java.util.Map;
import net.codejava.spring.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; 
@Controller
@RequestMapping(value = "/index")
public class indexController {  
   @RequestMapping(method = RequestMethod.GET)
   public String viewLogin(Map<String, Object> model) {
        User userForm = new User();     
        model.put("userForm", userForm);
        return "index";
    }
    @RequestMapping(method = RequestMethod.POST)
    public String processIn(@ModelAttribute("userForm") User user, 
            Map<String, Object> model) {
        System.out.println("username: " + user.getUsername());
        System.out.println("password: " + user.getPassword());
        return "indexSuccess";
    }
}

LoginController.java

package net.codejava.spring.controller;
import java.util.Map;
import net.codejava.spring.model.User;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
    @RequestMapping(value = "/login")
    public class LoginController {
    @RequestMapping(method = RequestMethod.GET)
    public String viewLogin(Map<String, Object> model) {
        User userForm = new User();     
        model.put("userForm", userForm);
        return "Login";
    }   
    @RequestMapping(method = RequestMethod.POST)
    public String processLogin(@ModelAttribute("userForm") User user,
            Map<String, Object> model) {
        System.out.println("username: " + user.getUsername());
        System.out.println("password: " + user.getPassword());      
        return "indexSuccess";
    }
}

RegisterController.java

package net.codejava.spring.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.codejava.spring.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/register")
public class RegisterController {
    @RequestMapping(method = RequestMethod.GET)
    public String viewRegistration(Map<String, Object> model) {
        User userForm = new User();     
        model.put("userForm", userForm);        
        List<String> professionList = new ArrayList<>();
        professionList.add("Developer");
        professionList.add("Designer");
        professionList.add("IT Manager");
        model.put("professionList", professionList);        
        return "Registration";
    }   
    @RequestMapping(method = RequestMethod.POST)
    public String processRegistration(@ModelAttribute("userForm") User     user, 
            Map<String, Object> model) {
        System.out.println("username: " + user.getUsername());
        System.out.println("password: " + user.getPassword());
        System.out.println("email: " + user.getEmail());
        System.out.println("birth date: " + user.getBirthDate());
        System.out.println("profession: " + user.getProfession());      
        return "RegistrationSuccess";
    }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org     /tags/form"%>    
 <!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>Registrazione e Login</title>
     </head>
     <body>
         <form:form method="post" action="login" commandName="userForm">
             <div align="center">
             Benvenuto: ${userForm.username}
             <table border="1" >
                 <thead>
                     <tr>
                         <th colspan="2">Login </th>
                     </tr>
                 </thead>
                 <tbody>
                     <tr>
                         <td>User Name</td>
                         <td><input type="text" name="uname" value=""   /></td>
                     </tr>
                     <tr>
                         <td>Password</td>
                         <td><input type="password" name="pass" value="" /></td>
                     </tr>
                     <tr>
                         <td><input type="submit" value="Login" /></td>
                         <td><input type="reset" value="Reset" /></td>
                     </tr>
                     <tr>
                         <td colspan="2">Non sei registrato? <a      href="register">Registrati qui</a></td>
                     </tr>
                 </tbody>
             </table>
             </div>
         </form:form>
     </body>
 </html>

indexSuccess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>    
<!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>Login Success</title>
</head>
<body>
    <div align="center">
        <table border="0">
            <tr>
                <td colspan="2" align="center"><h2>Index Successo!    </h2></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <h3>Benvenuto: ${userForm.username}</h3>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>

Login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>    
<!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>Login</title>
</head>
<body>
    <div align="center">
        <form:form action="login" method="post" commandName="userForm">
            <table border="0">
                <tr>
                    <td colspan="2" align="center"><h2>Spring MVC Form  Demo - Login</h2></td>
                </tr>
                <tr>
                    <td>User Name:</td>
                    <td><form:input path="username" /></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><form:password path="password" /></td>
                </tr>               
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="Login" /></td>
                </tr>
            </table>
        </form:form>
    </div>
</body>
</html>

LoginSuccess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>    
<!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>Login Success</title>
</head>
<body>
    <div align="center">
        <table border="0">
            <tr>
                <td colspan="2" align="center"><h2>Loggato con Successo!    </h2></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <h3>Benvenuto: ${userForm.username}</h3>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>

指数-servlet.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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--     <context:annotation-config /> -->
<!--     <context:component-scan     base-package="net.codejava.spring.controller.*" /> -->
        <bean id="viewResolver"          class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"     value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

web.xml

您的代码中有两个错误。

  1. 首先,您没有将 model 数据添加到 request

  2. 其次,您没有在 LoginSuccess 页面中指定 commandName 属性和 <form> 标记。

如果您只是想在成功页面中打印用户名,请在 request 中添加属性并简单地使用 el m 打印它,而无需寻求 modelAttribute.[=19 的帮助=]

@RequestMapping(method = RequestMethod.POST)
    public String processLogin(@ModelAttribute("userForm") User user,
            Map<String, Object> model) {
        System.out.println("username: " + user.getUsername());
        System.out.println("password: " + user.getPassword());  
        model.put("userName",user.getUsername() ) ;  
        return "indexSuccess";
    }

并且在 ,

indexSuccess.jsp

 ${userName}