Search code examples
javaspringrequest-mapping

Spring MVC passes null value on next page


I am a beginner in SpringMVC JSP and I am creating a simple project that passes values using POST Method.

1st page: hello.jsp

<body>
<h1>Record Form</h1>
    <form name="test" id="test" action="test.jsp" method="post">
        <p>Name: <input type = "text" name = "name" /></p>
        <p>Address: <input type = "text" name = "address" /></p>
        <p>Remarks: <input type = "text" name = "remarks" /></p>
        <p><input type="submit" value="Save" /> <input type="reset" value="Reset" /></p>
    </form>
</body>

2nd page: test.jsp

<body>
<h1>Result</h1>
    <p>name: ${record.name}</p>
    <p>address: ${record.address}</p>
    <p>remarks: ${record.remarks}</p>
    <a href="hello.jsp">Submit another message</a>
</body>

Record.java

import org.springframework.stereotype.Component;

@Component
public class Record {

    private String name;
    private String address;
    private String remarks;
 //setters getters..

HelloController.java

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

import org.springframework.beans.factory.annotation.Autowired;


@Controller
public class HelloController {

    @Autowired


    @RequestMapping(value = "/")
    public String hello(Record record) {
        return "hello";
    }

    @RequestMapping(value = "/test", method = POST)
    public String test(@RequestParam("name") String name, @RequestParam("address") String address, @RequestParam("remarks") String remarks, Model model) {
        Record record = new Record();
        record.setName(name);
        record.setAddress(address);
        record.setRemarks(remarks);
        model.addAttribute("record", record);
        return "/test";
    }
}

My problem is that when I click submit, no value was passed. I have been checking my code and I couldn't see what's wrong. Can anyone help me out please?

enter image description here

When changing codes to <form action="test"> only, this error occurs. Also if I changed to this <form action="/test"> based on what I have searched. Nothing works. See image below. enter image description here

and I already have this in web.xml

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

for my pom.xml, i already added this

 <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>
    </dependencies>

Solution

  • try these things MAC

    Change your web.xml like this:

    <welcome-file-list> 
     <welcome-file>hello.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
    <servlet-name>mvc</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>mvc</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/mvc-servlet.xml 
     </param-value> 
    </context-param>
    

    Make sure you have mvc-servlet.xml under /WEB-INF/ location

    Also, specify the location of jsp file in mvc-servlet.xml like

    <bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/" /> 
    <property name="suffix" value=".jsp" /> 
    </bean>
    

    (@MAC - in your case all your jsp file are at root level.)

    This might help you to solve the issue.