Spring MVC from example not working

I am new in Spring i am trying to develop a spring application where in a jsp page some information about a student will be given and when we will press the submit button then the the information will back in another jsp page,this is my moto.I have done this with eclise ide.But when i try to run this example the 404 is coming

I am posting my full code here .

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>HelloWeb</display-name>

  <servlet>
    <servlet-name>HelloWeb</servlet-name>
    <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
   </servlet>

 <servlet-mapping>
    <servlet-name>HelloWeb</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

HelloWeb-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<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"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

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

 </beans>

bean class and Controller under the same package com.tutorialspoint

Student.java is the bean class

public class Student {
private Integer age;
private String name;
private Integer id;

public void setAge(Integer age) {
    this.age = age;
}

public Integer getAge() {
    return age;
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setId(Integer id) {
    this.id = id;
}

public Integer getId() {
    return id;
}
}

StudentController.java is the controller class

import org.springframework.stereotype.Controller;
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 org.springframework.ui.ModelMap;

   @Controller
   public class StudentController {

     @RequestMapping(value = "/student", method = RequestMethod.GET)
      public ModelAndView student() {
      return new ModelAndView("student", "command", new Student());
    }

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    public String addStudent(@ModelAttribute("SpringWeb")Student student, 
    ModelMap model) {
      model.addAttribute("name", student.getName());
      model.addAttribute("age", student.getAge());
      model.addAttribute("id", student.getId());

      return "result";
   }
}

Here jsp folder under WEB-INF is the view we have two page there student.jsp & result.jsp

student.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Student Information</h2>
<form:form method="POST" action="/HelloWeb/addStudent">
 <table>
   <tr>
    <td><form:label path="name">Name</form:label></td>
    <td><form:input path="name" /></td>
</tr>
<tr>
    <td><form:label path="age">Age</form:label></td>
    <td><form:input path="age" /></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>

result.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
  <title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Submitted Student Information</h2>
  <table>
<tr>
    <td>Name</td>
    <td>${name}</td>
</tr>
<tr>
    <td>Age</td>
    <td>${age}</td>
</tr>
<tr>
    <td>ID</td>
    <td>${id}</td>
 </tr>
   </table>  
   </body>
   </html>

This is the full code what I have tried I have also added the Spring library.But when I try to run it I always get a 404 error.Someone please help

Answers


In your HelloWeb-servlet.xml, you need to configure your MVC environment. Do that with

<mvc:annotation-driven />

This configuration element registers any bean it finds which is a @Controller and has @RequestMappingmethods as a handler for HTTP requests.

You will need the appropriate XML namespace declarations.


it 's due to you are intercepting jsp files as well using <url-pattern>/</url-pattern> so Resolution would be like this.

  1. in web.xml use <url-pattern>*.htm</url-pattern>
  2. in student.jsp <form:form method="POST" action="/HelloWeb/addStudent.htm">

I worked through all the examples on that site a few months ago and remember them well.

@Sotirios Delimanolis, I believe the annotation-driven tag isn't necessary as long as you are using xml based configuration and doing component scanning. The @Controller annotation should still be registered. The annotation tag you suggested isn't in the original tutorial, or in my working example (still on my laptop) either, and it still runs. Also, it looks to me like all the XML namespace declarations from the tutorial XML are shown in the OP's [servlet-name]-servlet.xml file.

@Pankaj Sharma, the / notation in the web.xml is what's shown in the tutorial and works fine in my example, and my .jsp works with action="/HelloWeb/addStudent". Anyway, that jsp action should only effect going to the 'result' screen and the OP isn't even getting that far.

OP, first I would be sure you have all the jar files called for in the tutorial added in your build path. Also, I'm wondering if you are attempting to invoke your tutorial app correctly. From my MyEclipseBlue IDE, I can't get it to run even when left clicking and trying to run it as a 'MyEclipse server application'. The entire URL does not come up, it lacks the /student in the URL needed to match the pattern in the controller. If you read the example closely, it suggests starting up your server and opening up a new browser window, then entering the URL exactly as shown in the original tutorial. This works on my application, or else run it as a 'MyEclipse server application' and enter the full URL with /student appended.


Need Your Help

Can I reference intermediate nuget package versions without putting them into remote repository?

visual-studio visual-studio-2012 nuget nuget-package

I have 2 solutions in Visual Studio 2012. One solution is a dll project and another one is a desktop app which is using that dll.

Sprite Kit Child of Child of Child in Swift

swift sprite-kit

I'm using Swift and Sprite Kit. I have an SKNode called MrNode that has several SKSpriteNodes and SKNode child nodes. Some of the SKNodes have children that have children of their own as well. To