Sagar Kadu
Sagar Kadu

Reputation: 261

Post request parameters not getting displayed to jsp

I'm using Springs form tag library for submitting user form..I'm able to catch post request parameters in method handler but its not getting displayed in jsp where it needs to be printed whatever user has submitted..

Below is controller class

package com.sagar;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller

public class MyController {



    @RequestMapping(value="/edit",method=RequestMethod.GET)

    public String handleUserInput(Model m,@ModelAttribute("userLogin") User user){

        m.addAttribute(new User());

        return "edit";

    }


    @RequestMapping(value="/edit",method=RequestMethod.POST)

    public String handleUserPost(@ModelAttribute("userLogin") User user,Model mav ){
        mav.addAttribute("user",user);
        System.out.println(user.getFirstName());
        System.out.println(user.getLastName());
        System.out.println(user.getPassword());

        return "view";

    }

}

Web-context

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd ">


     <context:component-scan base-package="com.sagar"/>

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value>/WEB-INF/</value></property>
<property name="suffix"><value>.jsp</value></property>
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
</beans>

Below is my user class

package com.sagar;

public class User {

    private String firstName;
    private String lastName;
    private String password;

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

View.jsp (which should display whatever user has submitted).

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!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>Insert title here</title>
</head>
<body>
<table>
    <tr>
        <td>Name</td>
        <td>${firstName}</td>
    </tr>
    <tr>
        <td>LastName</td>
        <td>${lastName}</td>
    </tr>
    <tr>
        <td>password</td>
        <td>${password}</td>
    </tr>
</table> 
</body>
</html>

User Form(edit.jsp)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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>Welcome to Login Page</title>
</head>
<body>

<form:form modelAttribute="userLogin">
      <table>
          <tr>
              <td>First Name:</td>
              <td><form:input path="firstName" /></td>
          </tr>
          <tr>
              <td>Last Name:</td>
              <td><form:input path="lastName" /></td>
          </tr>
          <tr>
      <td>Password:</td>
      <td>
          <form:password path="password" />
      </td>
  </tr>
          <tr>
              <td colspan="2">
                  <input type="submit" value="Log In" />
              </td>
          </tr>
      </table>
  </form:form>
</body>
</html>

Please see this screenshot of output
Please see this screenshot of output

Upvotes: 0

Views: 87

Answers (1)

Blank
Blank

Reputation: 12378

You've bind your entity User to a key named user, so when you want get Object value, you should use this key ${user.firstName}.
Try to change your parameter for rendering your jsp, like this;)

<table>
  <tr>
    <td>Name</td>
    <td>${user.firstName}</td>
  </tr>
  <tr>
    <td>LastName</td>
    <td>${user.lastName}</td>
  </tr>
  <tr>
    <td>password</td>
    <td>${user.password}</td>
  </tr>
</table>

Upvotes: 1

Related Questions