2012-09-27 71 views
0

我在jsp/jstl中有名稱和密碼 我試圖將名稱和密碼傳遞給控制器​​。如何將變量值從jstl傳遞到控制器

這是我的控制器類

package com.simple.controller;

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

@Controller 
@RequestMapping("/welcome") 
public class HelloController { 
    private String name; 
    private String password; 
     private String user; 
    @RequestMapping(method = RequestMethod.POST) 
    public String printWelcome(ModelMap model) { 
      System.out.println(user); 
     model.addAttribute(name); 
     model.addAttribute(password); 

     model.addAttribute("message", "Spring 3 MVC Hello World"); 
     return "hello"; 

    } 

} 

這是用的index.jsp form標籤和c標記庫

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
     pageEncoding="ISO-8859-1"%> 
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
    <!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>Registration Page</title> 
    </head> 
    <body bgcolor="#999966"> 
<p>&nbsp;</p> 
<form method="POST" action="welcome"> 

<p><font color="#800000" size="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
UserName:</font><input type="text" name="name" size="20"></p> 
<c:set var="user" value="{param.name}" scope="request"> 
</c:set> 

<p><font color="#800000" size="5"> 
password:</font><input type="text" name="password" size="20"></p> 
<p><input type="submit" value="Submit" name="B1"></p> 
</form> 
</body> 
    </html> 

我試圖通過從這種形式到控制器變量用戶(該值從名稱變量取)。

<c:set var="user" value="{param.name}" scope="request"> 
    </c:set> 

任何機構可以幫助我如何用C標記來做到這一點..

我已經使用的CommandName =「registereduser」,其中用戶類RegisteredUser的對象來完成。

,但我只傳遞變量(以c標記) 我在系統輸出得到空值的用戶

有沒有辦法做到與集合C標籤試圖

謝謝..

回答

2

在你的控制器:

@Controller 
@RequestMapping("/welcome") 
public class HelloController { 

    @RequestMapping(method = RequestMethod.POST) 
    public String printWelcome(ModelMap model, @RequestParam String name, @RequestParam String password) {  
     // do something with name & password 
     model.addAttribute(name); 
     model.addAttribute(password); 
     model.addAttribute("message", "Spring 3 MVC Hello World"); 
     return "hello"; 
    } 
} 

,並在你的JSP(你必須使用一個普通的HTML表單):

<form method="POST" action="welcome"> 
    <table> 
     <tr> 
      <td>User Name :</td> 
      <td><input type="text" id="name" name="name"/></td> 
     </tr> 
     <tr> 
      <td>Password :</td> 
      <td><input type="password" id="password" name="password"/></td> 
     </tr> 

     </table> 
     <tr> 
      <td colspan="2"><input type="submit"></td> 
     </tr> 
    </table> 
</form> 

EDIT問題(用戶變量中加入):
您必須通過user與隱藏的輸入到控制器,並添加另一@RequestParam到控制器的方法:

JSP:

<input type="hidden" id="user" name="user" value="${name}"/> 

控制器方法:

public String printWelcome(ModelMap model, @RequestParam String name, 
@RequestParam String password, @RequestParam String user) { 
     ... 

我認爲你不能發送user價值服務器與C標籤,你需要提交數據(表格)到控制器。

相關問題