Spring RequestMapping using PathVariable results in 404 - spring

I have a spring application and I can't solve a problem. When I use a PathVariable, the value in my RequestMapping is getting added to the URI and the DispatchHandler is getting confused.
For example, when I request /Dashboard/project/1131/
I get HTTP Status 404 - /Dashboard/project/1131/WEB-INF/jsp/projectDetail.jsp
The project/1131 gets added into the path for some reason
When I request /Dashboard/projects Spring finds my .jsp and presents it. /project/{projectId} doesn't work - I get the behavior described above.
Here's my controller
#Controller
public class ProjectController {
protected final Log logger = LogFactory.getLog(getClass());
#RequestMapping(value = "projects", method=RequestMethod.GET)
public String projects() {
return "/projects";
}
#RequestMapping(value="/project/{projectId}", method=RequestMethod.GET)
public String project(#PathVariable("projectId") String projectId, ModelMap map) {
map.put("projectId", projectId);
logger.info("Project Id: " + projectId);
return "/projectDetail";
}
}
My configuration
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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Webapp name -->
<display-name>Dashboard</display-name>
<!-- default file name -->
<welcome-file-list>
<welcome-file>home</welcome-file>
</welcome-file-list>
<!-- spring listener (all spring jars need to be in WEB-INF/lib -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- set servlet to DispatcherService -->
<servlet>
<servlet-name>dashboard</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- hand all requests to dispatcher service -->
<servlet-mapping>
<servlet-name>dashboard</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
AppConfig
#Configuration
public class AppConfig {
#Bean
ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}
dashboard-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- the packages to scan for annotations -->
<context:component-scan base-package="com.dennisstevens.dashboard.controller" />
<context:component-scan base-package="com.dennisstevens.dashboard" />
<context:component-scan base-package="com.dennisstevens.dashboard.domain" />
<!-- Tell Spring that MVC components are #Annotation Driven -->
<mvc:annotation-driven />
<!-- Tell Spring Dispatch Handler to look in WebContent/resources/ for resources -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Tell Spring to scan for config annotations -->
<context:annotation-config/>
<!-- TODO: Figure out how to load this in AppConfig -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
</beans>
This has to be simple - but I have looked through the documentation and searched the internet for hours. It looks like I am doing everything right from what I can see.

I think having a leading slash in resolver.setPrefix("/WEB-INF/jsp/"); should fix it for you.

Related

Spring framework configuration

It has been 3 days since I started learning about Spring Framework and trying to implement a RESTful web service with MongoDB and Spring Framework. I am still at the beginning and trying to understand the configuration of Spring Framework. When I start my project and hit the desired URL it is not working. I have also upload my project to github (here is the url)
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
id="WebApp_ID"
version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
For example #Controller and #Service. Make sure to set the correct base-package-->
<context:component-scan base-package="shoponway.webservice" />
<!-- Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />
<!-- Loads MongoDB configuraton -->
<import resource="mongo-config.xml" />
</beans>
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>
and my controller
#Controller
public class PersonController {
protected Logger logger = Logger.getLogger(PersonController.class);
#Resource(name = "personService")
private PersonService personService;
#RequestMapping(value = "/allpersons", method = RequestMethod.GET)
public String getAllPersons(Model model){
model.addAttribute("persons", personService.getAllPersons());
return "personspage";
}
}
The error I am getting
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:529)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:511)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:139)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4888)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
There are multiple problems in this example, which can be corrected as follows
add #Service anotation to your shoponway.webservice.services.PersonService class, you need to add #Service for making it a proper candidate for injection.
Your 'mongo-config.xml' is pointing to wrong mongo repository locations edit it to
<mongo:repositories base-package="shoponway.webservice.services" />
from
<mongo:repositories base-package="org.krams.tutorial.repositories" />
Mongo template should refer to following bean <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> not with class="org.springframework.data.document.mongodb.MongoTemplate"
4) replace mongo-config.xml content with
<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:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<mongo:mongo host="localhost" port="27017" />
<mongo:db-factory dbname="shoponwaydb" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>
`
Once your correct these , you will be able to run your example easily:

Can't use freemarker with Spring MVC 3

I created a simple Spring MVC 3 application and want to use freemarker template engine. I cofigure *-context.xml as describes in off Spring's docs, but in browser I get 404 Page not found error. this is my code:
HelloWorldController.java
#Controller
#RequestMapping("/hello")
public class HelloWorldController {
private static final Logger log = Logger.getLogger(HelloWorldController.class);
#RequestMapping(value="/{name}", method = RequestMethod.GET)
public String hello(#PathVariable String name, Model model) {
String result = "Hello, " + name;
model.addAttribute("result", result);
return "hello";
}
}
this is my hello.ftl in WEB-INF/freemarker folder
<!doctype html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${result}. </P>
</body>
</html>
and my servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- freemarker config -->
<beans:bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<beans:property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</beans:bean>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<beans:property name="prefix" value="" />
<beans:property name="suffix" value=".ftl" />
<beans:property name="cache" value="false" />
</beans:bean>
<context:component-scan base-package="org.example.simple" />
</beans:beans>
what is wrong and why I get 404 when I go to localhost:8080/simple/hello/username ?
please, help
EDIT:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
its probably wrong configuration for spring mvc, not freemarker. Seems that spring cant find your Controller. Did you add dispatcherServlet in your web.xml?

bean not injecting on spring MVC

im learning Spring MVC and i was working on a basic form example, but i dont why a bean its not injecting the information correctly so i would like to know if someone can direct me.
The controller
package com.carloscortina.Test;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.carloscortina.toy.model.Member;
#Controller
public final class NomineeController {
private static final Logger log=
Logger.getLogger(NomineeController.class);
private String thanksViewName ="thanks";
public void setThanksViewName(String thanksViewName) {
this.thanksViewName = thanksViewName;
}
#RequestMapping(method = RequestMethod.GET)
public Member form() { return new Member();}
#RequestMapping(method = RequestMethod.POST)
public String processFormData(Member member){
log.info("Processing nominee: " + member);
log.info("thanksViewName: " + thanksViewName);
return thanksViewName;
}
the root-context.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="formAnswer"
class="com.carloscortina.Test.NomineeController"
p:thanksViewName="thanks" />
</beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.carloscortina.Test" />
im using STS as IDE and the Spring mvc template included.
im not sure the bean its not being injected in the controller so when the form its submitted it redirects correctly, if i hardcode the thanks in the controller it works.
Thanks in advance for the help i know that this might be a basic error , so thank you.
*Edit
Well, maybe it related to the annotation driven, but I'm still not sure, i havent been able to make this thing work.
So the controller cant rad a bean from the root-context.xml?
or anyone can tell me how to do it with auto wire, the idea its just to not hardcode the value of thanksViewName on the controller.
You need to add the following to your servlet-context.xml configuration file, you don't need to declare annotated beans in the XML file.
<mvc:annotation-driven />
<context:component-scan base-package="com.carloscortina" />
My suggestion would be to download Spring Stool Suite (STS) and create a new spring template project (selecting the MVC template), it will create a runnable project and that way you can see how everything is put together.

Spring MVC example not working

I'm in trouble and would like your help. I'm a beginner in Spring MVC (and Spring at all). I have followed the http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/ but it isn't working on. I added a welcome file (index.jsp). When i enter (http://localhost:8080/SpringMVC) all right. But when i add the controller pattern (http://localhost:8080/SpringMVC/welcome), it doesn't work (HTTP Status 404). Here my configs:
web.xml
<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>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
mvc-dispatcher-servlet.xml
<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.mkyong.common.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
and my folder structure is:
-> src
-> main
-> java
-> com
-> mkyong
-> common
-> controller
-> HelloController.java
-> resources
-> webapp
-> index.jsp
-> WEB-INF
-> mvc-dispatcher-servlet.xml
-> web.xml
-> pages
-> hello.jsp
Someone can help me?
404 means that the requested resource cannot be found. Make sure your controller is annotated with:
#Controller and #RequestMapping("/welcome")
From the link:
#Controller
#RequestMapping("/welcome")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
You have your servlet mapped to '/', and the controller you want to hit is accepting requests for '/welcome'
The request you need to make should be (http://localhost:8080/welcome). I don't know why he has that extra 'SpringMVC' in his example.
Also, add <mvc:annotation-driven/> to your mvc-dispatcher-servlet.xml to make sure it's using annotations on your controller(s). And add the mvc XML namespace also. My namespaces look like this:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
I would suggest getting rid of that welcome file until you have your controller working correctly to keep things simple
You must have a Controller mapped to "/".
#Controller
#RequestMapping("/")
public class StartController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
Include this library on your view to enable ${}
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
This will solve your problem.

No mapping found for HTTP request with URI in DispatcherServlet with name 'dispatcher'

I'm new to Spring framework. I just started implementing multiaction controller in netbeans. But. I'm getting the above error. I'm pasting my code below. Plz take a look into it and resolve me the issue.
dispatcher-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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean name="/*.htm" class="controller.MyController"/>
</beans>
index.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
<h4>Multi Action Controller Example</h4>
Add
Update
Remove
</body>
</html>
MyController.java:
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class MyController extends MultiActionController {
public ModelAndView add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
System.out.println("Add ma");
return new ModelAndView("result","message","Add Method Called");
}
public ModelAndView update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
System.out.println("Update ma");
return new ModelAndView("result","message","Update Method Called");
}
public ModelAndView remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
System.out.println("Remove ma");
return new ModelAndView("result","message","Remove Method Called");
}
}
result.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Nee Varaatha</title>
</head>
<body>
<h1>Please Show it </h1>
${message}
</body>
</html>
Try checking your apachelog in the output Window of netbeans and see what's the default mapping of your controller in it, and append that mapping in the index.jsp.
For example in the log you'll find
Mapped URL path [/employee] onto handler '/*.html'
Simply append employee/add.htm in the JSP
Have you considered using #Controller annotated classes? Since spring 2.5 annotations are usually preferred.
Here's an example web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" 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_5.xsd">
<display-name>Example</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/example-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
example-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!--Tell the servlet where to look for annotated methods-->
<context:component-scan base-package="controller" />
<!--if no controller logic is required, mvc:view-controller can be used to simply show a view for a request -->
<mvc:view-controller path="/" view-name="index"/>
<!--Enables many annotations and searches for #Controller annotated methods etc.. -->
<context:annotation-config />
<!--JSR-303 (Bean validation) support will be detected on classpath and enabled automatically-->
<mvc:annotation-driven />
<!--This tag allows for mapping the DispatcherServlet to "/" (all extensions etc)-->
<mvc:default-servlet-handler/>
<mvc:resources location="/resources/**, classpath:resources" mapping="/resources/**"/>
<!--Configures the application to search for views in folder /WEB-INF/jsp/ with the suffix ".jsp"
in controllers prefix and suffix are therefore no longer needed-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
And an example simple controller:
#Controller
public class ExampleController {
#RequestMapping("/test")
public String test(Model model) {
model.addAttribute("message","Test message");
return "result";
}
}
Also see the spring reference documentation here

Resources