Reputation: 329
I'm being able to show my form from my student.jsp file and when I click on the submit button, it is supposed to be redirected to the main.jsp file with the results of the inputs I have given in the previous page, ie. in the form. But the main.jsp page is not showing, rather I'm getting a 404.
Here is the NoticesController.java:
package com.mvc.spring;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class NoticesController {
@RequestMapping(value = "/student", method = RequestMethod.GET)
public String showHello(Model model)
{
return "student";
}
@RequestMapping(value = "/main", method = RequestMethod.POST)
public String form(@ModelAttribute("StudentModel") Student student, Model model)
{
System.out.println("Inside form");
model.addAttribute("Student", student);
return "main";
}
}
This is Student.java:
package com.mvc.spring;
import org.springframework.stereotype.Controller;
public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
This is my student.jsp:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>This is student.jsp</title>
</head>
<body>
<form action="main.jsp" method="POST">
ID: <input type="text" name="id"><br />
Name: <input type="text" name="name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
This is my main.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Using GET and POST Method to Read Form Data</title>
</head>
<body>
<p>${Student.id}</p>
<p>${Student.name}</p>
</body>
</html>
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>WebMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>notices</display-name>
<servlet-name>notices</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>notices</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<description>Spring Tutorial</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/spring</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
This is my notices-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.mvc.spring"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="jspViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
This is the log:
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/notices/] in DispatcherServlet with name 'notices'
Upvotes: 2
Views: 8747
Reputation: 329
I solved my problem in the following way:
This is the Controller class:
package com.mvc.spring;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
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.servlet.ModelAndView;
import com.mvc.spring.database.Student;
import com.mvc.spring.service.StudentService;
@Controller
public class NoticesController {
@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView showHello(Model model) {
return new ModelAndView("student", "command", new StudentSazz());
}
@RequestMapping(value = "/main", method = RequestMethod.POST)
public String form(@ModelAttribute("StudentModel") StudentSazz student,
ModelMap model) {
System.out.println("Inside form");
model.addAttribute("name", student.getName());
model.addAttribute("id", student.getId());
model.addAttribute("student", student);
return "main";
}
}
This is the student.jsp file:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>This is student.jsp</title>
</head>
<body>
<h2>Student Information</h2>
<form:form method="POST" action="/notices/main">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="id">id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
This is the main.jsp file:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Using GET and POST Method to Read Form Data</title>
</head>
<body>
<h2>Submitted Student Information</h2>
<table>
<tr>
<td>Name</td>
<td>${name}</td>
</tr>
<tr>
<td>ID</td>
<td>${id}</td>
</tr>
</table>
</body>
</html>
Basically, what I did is I used the Student object in the controller class to get all the parameters and used them in the .jsp files with just the $ notation. This worked fine.
Upvotes: 4
Reputation: 1479
What if you change form-action like this:
<form action="/main" method="POST">
Upvotes: 1