1

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>
MAC
  • 676
  • 3
  • 13
  • 32
  • @Mohale still not working – MAC Dec 06 '19 at 08:36
  • Do you have a `reservation-page.jsp` in your `/WEB-INF/jsp/` directory? – M. Deinum Dec 06 '19 at 10:02
  • @MAC please refer, https://stackoverflow.com/q/22405212/8700934 – Lakshan Dec 07 '19 at 04:44
  • @M.Deinum yes I have. – MAC Dec 10 '19 at 01:54
  • @Lakshan Still did not help. Is it possible that the cause is because I am missing Java Path Entry? – MAC Dec 10 '19 at 01:55
  • @MAC, do I understand it correctly that the part of your question about the redirect is irrelevant? I. e. you just need to solve the problem of getting error 404 when going to `http://localhost:8080/SpringMVCTextField/reservation/bookingForm`, right? – Petr Bodnár Dec 10 '19 at 09:24
  • I change my question to a simpler one. I tried to make it from start so I can see what went wrong when passing that values. – MAC Dec 10 '19 at 09:45
  • 1
    Oh, so you've changed it completely... :) Your original question linked to [a tutorial](https://www.javatpoint.com/spring-mvc-form-text-field) - I would recommend you *not* to use it at the 1st place. It is full of mistakes right from the start, invalid dependencies in pom.xml, missing Java version 1.7+ requirement, etc. – Petr Bodnár Dec 10 '19 at 10:06
  • @PetrBodnár Thank you so much for telling me that. I was really confused what went wrong with my first one. Now I will proceed with my new issue. Thanks again. – MAC Dec 11 '19 at 01:07

2 Answers2

2

Basically, you need to change this:

<form name="test" id="test" action="test.jsp" method="post">

To this:

<form name="test" id="test" action="test" method="post">

Otherwise your controller method mapped at /test will not be called and test.jsp is rendered directly, with the record model variable being empty.

Petr Bodnár
  • 496
  • 3
  • 14
  • An error 404 occurs when I don't add `.jsp` on `action` part – MAC Dec 11 '19 at 01:14
  • i updated my question, adding the results when i change code to `action="test"` and i also added my web.xml and pom.xml, can you please check and see if you can tell me what went wrong? – MAC Dec 11 '19 at 01:27
0

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.

Atul
  • 3,043
  • 27
  • 39
  • if i add those codes to web.xml and spring-servlet.xml, the index.jsp will now return error 404 as well @Atul – MAC Dec 06 '19 at 08:46
  • I am assuming you have mentioned the index.jsp in welcome file list in web.xml file. If not then add it ** index.jsp ** – Atul Dec 06 '19 at 09:07
  • This will only load the same configuration twice and will not solve the issue. – M. Deinum Dec 06 '19 at 10:01
  • The error message shows that, you have to use at lease Java 1.7 or more. Configure required version of Java and run the application again. – Atul Dec 06 '19 at 10:03
  • As I mentioned in the answer, add index.jsp file as a welcome file. In index.jsp you have to redirect it to the location you want like, ** <%response.sendRedirect("reservation/bookingForm"); %> ** . One question, if you are accessing full path, is it working? – Atul Dec 10 '19 at 05:42
  • One more question, where is your index.jsp file, is it in /WEB-INF/jsp folder? If it is there then you have to move the file to the level where WEB-INF folder is and then try. – Atul Dec 10 '19 at 05:47
  • @Atul I think my Controller is not called from View – MAC Dec 11 '19 at 03:42
  • If I use `action="test.jsp"` it will display next page but no values. if i change to `action="test"`, error 404 appears. can you tell me what was wrong? i am already using `/` in web.xml – MAC Dec 11 '19 at 03:44
  • @MAC, please try "/test" instead of "test" only. – Atul Dec 11 '19 at 05:00
  • @Atul I did, still no – MAC Dec 11 '19 at 05:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204022/discussion-between-atul-and-mac). – Atul Dec 11 '19 at 05:27
  • Good that you resolved it in the end. A near-pro tip: Don't forget to specify in the answer what was actually wrong - I suppose it was a misconfigured Spring in this case? @MAC, also it will help you a lot if you add logging (like slf4j / log4j) to your app next time, so you can see what's going on (wrong) in your app... – Petr Bodnár Dec 11 '19 at 08:22