本文介绍了如何将变量值从jstl传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jsp / jstl中有名称和密码
i尝试向控制器传递名称和密码。



这是我的控制器类



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);
returnhello;

}

}

.jsp with form tag and c tag libraries

 <%@ page language =javacontentType =text / html; charset = ISO-8859-1
pageEncoding =ISO-8859-1%>
<%@ taglib uri =http://www.springframework.org/tags/formprefix =form%>
<%@ taglib prefix =curi =http://java.sun.com/jsp/jstl/core%>
<!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org/TR/html4/loose.dtd\">
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = ISO-8859-1>
< title>注册页< / title>
< / head>
< body bgcolor =#999966>
< p>& nbsp;< / p>
< form method =POSTaction =welcome>

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

< p>< font color =#800000size =5>
password:< / font>< input type =textname =passwordsize =20>< / p&
< p>< input type =submitvalue =Submitname =B1>< / p&
< / form>
< / body>
< / html>

我试图从此表单传递变量user(值取自name变量)到控制器。

 < c:set var =uservalue ={param.name}scope = > 
< / c:set>

可以使用c标签来帮助我。



我使用commandName =registereduser,其中user是类RegisteredUser的对象。



但我试图只传递变量(带有c标签)
我在sysout中获取用户的空值



有任何方法可以使用c标签



谢谢..

解决方案


$ b

  @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);
returnhello;
}
}

HTML表单):

 < form method =POSTaction =welcome> 
< table>
< tr>
< td>用户名:< / td>
< td>< input type =textid =namename =name/>< / td>
< / tr>
< tr>
< td>密码:< / td>
< td>< input type =passwordid =passwordname =password/>< / td>
< / tr>

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

EDIT TO QUESTION(添加用户变量): $ b您需要将 user 传递给控制器​​,并将输入隐藏并向控制器方法添加另一个 @RequestParam

JSP:

 < input type =hiddenid = username =uservalue =$ {name}/> 

控制器方法:

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

我想你不能使用c标签向服务器发送用户向控制器提交数据(表单)。


I have name and password in jsp/jstl i tried to pass name and password to controller.

this is my controller class

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";

    }

}

this is index.jsp with form tag and c tag libraries

  <%@ 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>

i am trying to pass the variable user( the value is taken from the name variable) from this form to controller.

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

can any body help me how to do this with c tags..

i have done with using commandName="registereduser" where user is the object of class RegisteredUser.

But i am trying with just passing the variable (with c tags)I am getting null value for user in sysout

is there any way to do with c tags with the set

thank you..

解决方案

In your controller:

@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";
    }
}

and in your JSP (you have to use a regular HTML form):

<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 TO QUESTION (user variable added):
You need to pass user to controller with an input hidden and add another @RequestParam to controller method:

JSP:

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

Controller method:

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

I think you can't send user value to server with c tags, you need to submit data (form) to controller.

这篇关于如何将变量值从jstl传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:19