2014-09-24 69 views
0

在使用下面的servlet获取表单值时,如果表单已发布,即可获取用户名和密码,但无法在显示用户名/密码的mainpage.jsp中访问该表单。Spring无法获取表单值

Servlet 

package com.school.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

import com.school.beans.Login; 

@Controller 
public class Logincontroller { 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public ModelAndView login() { 
     return new ModelAndView("login", "loginform", new Login()); 
    } 

    @RequestMapping(value = "/validatelogin", method = RequestMethod.POST) 
    public String validatelogin(@ModelAttribute("SchoolManagement")Login login, 
       ModelMap model) { 
     model.addAttribute("username", login.getUsername()); 
     model.addAttribute("password", login.getPassword()); 
     System.out.println("useranme = " + login.getUsername()); 
     System.out.println("password = " + login.getPassword()); 
     return "mainpage"; 
    } 
} 

的login.jsp

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<html> 
<head> 
<!-- 
<script src="javascript/login.js"></script> 
<link rel="stylesheet" type="text/css" href="css/login.css"/> 
http://www.mkyong.com/spring-mvc/spring-mvc-how-to-include-js-or-css-files-in-a-jsp-page/ 
--> 
<!--<script src="<c:url value="/resources/js/jquery.1.10.2.min.js" />"></script>--> 
<link href="<c:url value="/resources/css/login.css" />" rel="stylesheet"> 
<script src="<c:url value="/resources/js/login.js" />"></script> 
<script 
    src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
</head> 
<body> 
    <div id="top"></div> 
    <div id="middle"> 
     <form:form method="POST" id="loginform" commandName="loginform" 
     action="/SchoolManagement/validatelogin"> 
      <form:label path="username"> Username:</form:label> 
      <form:input path="username" /> <br> 
      <form:label path="password"> Password:</form:label> 
      <form:input path="password" /> 
      <input type="submit" value="submit"> 
     </form:form> 
    </div> 
    <div id="bottom"></div> 

</body> 
</html> 

mainpage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="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=ISO-8859-1"> 
<title>MainPage</title> 
</head> 


<body> 

    <h2>Submitted Student Information</h2> 
    <table> 
     <tr> 
      <td>Name</td> 
      <td>${username}</td> 
     </tr> 
     <tr> 
      <td>Password</td> 
      <td>${password}</td> 
     </tr> 
    </table> 
</body> 
</html> 
+0

粗略地看一眼看起来像@ModelAttribute的名称有误 – 2014-09-24 22:48:56

+0

什么应该是@modelattribute名称? – user1050619 2014-09-24 22:49:40

+0

与传递给表单的模型相同,以及与commandName属性绑定的内容相同,因此“loginform” – 2014-09-24 22:50:34

回答

0

尝试直接得到的值出请求的。

<% 
    String username = (String) request.getAttribute("username"); 
    String password = (String) request.getAttribute("password"); 
%> 

<table> 
    <tr> 
     <td>Name</td> 
     <td><%= username %></td> 
    </tr> 
    <tr> 
     <td>Password</td> 
     <td><%= password %></td> 
    </tr> 
</table> 

这应该肯定会工作......如果不是,那么设置有问题。

0

你的命令名称

commandName="loginform" 

比模型属性不同

@ModelAttribute("SchoolManagement") 

尝试在帖子中的方法来使用相同的名称