How to pass variable value from jstl to controller

I have a username and password in jsp / jstl I tried to pass the username and password to the 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 tags and c tags

  <%@ 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 user a variable (value taken from a variable name) from this form to the 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 did using commandName = "registereduser", where the user is an object of the RegisteredUser class.

But I'm trying to just pass a variable (with c tags) I get a null value for the user in sysout

is there any way to do with c tags using a set

thank..

0
spring spring-mvc jstl controller
Sep 27 '12 at 12:25
source share
1 answer

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

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 that you cannot send the user value to the server using c tags, you need to send data (form) to the controller.

+2
Sep 27 '12 at 13:25
source share



All Articles