Pratap M
Pratap M

Reputation: 1081

How to pass variable value from jstl to controller

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..

Upvotes: 0

Views: 21665

Answers (1)

jelies
jelies

Reputation: 9290

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.

Upvotes: 2

Related Questions