如何将变量值从jstl传递给控制器

时间:2012-09-27 12:25:05

标签: spring spring-mvc controller jstl

我在jsp / 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";

    }

}

这是带有表单标记和c标记库的index.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"%>
    <%@ 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”,其中user是类RegisteredUser的对象。

但我正在尝试传递变量(带有c标签) 我在sysout

中获取用户的空值

有没有办法处理带有

的c标签 谢谢..

1 个答案:

答案 0 :(得分: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>

编辑问题(添加了用户变量):
您需要将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) { 
        ...

我认为您无法使用c标签向服务器发送user值,您需要向控制器提交数据(表单)。

相关问题