2014-09-24 71 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") 

嘗試在帖子中的方法來使用相同的名稱