Spring MVC - When I am click on Submit button on jsp (Post request) my URL changed and my work flow not coming into controller class - spring

When I am click on Submit button on jsp (Post request) my URL changed and my work flow not coming into controller class
First time when I hit URL http://localhost:9080/LaunchingGateway_Maintance/services/Test
my jsp will be opend but when I click on Vendor button on jsp URL will changed to http://localhost:9080/validate and my flow not comming to my controller and on browser I got The webpage cannot be found
please help me I am stuck here from last two week.
my Controller class is : -
package com.us.launchingGatewayController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.us.bean.SSOConfigDynaFldsSrveLstBean;
import com.us.validator.SSOconfigValidator;
#Controller
public class LaunchingGatewayController {
#Autowired
SSOConfigDynaFldsSrveLstBean SSOConfigDynaFldsSrveLstBean;
#Autowired
ModelAndView modelNView;
#Autowired
SSOconfigValidator SSOConfigValidator;
#RequestMapping(value = "/Test", method = RequestMethod.GET)
public ModelAndView test(HttpServletRequest httpRequest, HttpServletResponse resp)throws Exception {
System.out.println("Test In test");
modelNView.setViewName("test");
return modelNView;
}
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(){
System.out.println("hi");
return "test";
}
#RequestMapping(value = "/validate", method = RequestMethod.POST)
public ModelAndView validate(HttpServletRequest httpRequest, HttpServletResponse resp) throws Exception{
System.out.println("Test In Validate");
SSOConfigValidator.validateSSOconfig(SSOConfigDynaFldsSrveLstBean);
modelNView.setViewName("test");
return modelNView;
}
}
my jsp is :-
<%#page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Launching Gateway Maintenance</title>
<script language="JavaScript" type="text/javascript">
</script>
</head>
<body bgcolor='#E8E8E8'>
<h1 align="center">Launching Gateway Maintenance</h1>
<form method="POST" action="/validate">
<center>
<input type="submit" name="validate" value="Vendor" />
</center>
</form>
</body>
</html>
my web.xml is :-
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>LaunchingGateway_Maintance</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
my spring-dispatcher-servlet.xml is :-
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:util="http://www.springframework.org/schema/util"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<context:component-scan base-package="com.us.launchingGatewayController" />
<bean id="ssoConfigValidator" class="com.us.validator.SSOconfigValidator"></bean>
<bean id="modelNView" class="org.springframework.web.servlet.ModelAndView"></bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="../jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

Something wrong with you context path. According to form you submitting, try this:
<FORM ACTION="${pageContext.request.contextPath}/validate">
Compare this two urls :
http://localhost:9080/LaunchingGateway_Maintance/services/Test
and
http://localhost:9080/validate
So looks like you are missing some context path LaunchingGateway_Maintance
Check this post as well

Related

Cant able to run spring application?

I am new to spring i am trying to run the spring application, i am getting the DispatcherServlet class not found exception. The below is the code i used can anyone please help me? I tried many solution provided in stackoverflow and google but i didn't get answer for this problem. Thanks in advance.
package com.raistudies.actions;
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 org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldAction {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public ModelAndView sayHello(Model model) {
System.out.println(" hellow world ");
ModelAndView mav = new ModelAndView();
mav.setViewName("hello");
model.addAttribute("helloMessage",
"Hello World from my spring 3 mvc application");
return mav;
}
}
hello.jsp - WEB-INF/jsp/
<html>
<head>
<title>Hello world with spring 3 mvc </title>
</head>
<body>
<h1>Welcome! Spring MVC is working well.</h1><br />
${helloMessage}
</body>
</html>
index.jsp - WEB-INF
<html>
<head>
<title>rai studies</title>
</head>
<body>
Welcome...
<br>Click here to check the output :-)
</body>
</html>
app-config.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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.raistudies.actions" />
<context:annotation-config />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
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"
id="WebApp_ID" version="3.0">
<display-name>HWEWS3MVCIE</display-name>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springfrmaework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
My dependencies:
commons-logging-1.1.2.jar
jstl-1.2.jar
log4j-1.2.16.jar
spring-aspects-3.2.5.RELEASE.jar
spring-beans-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-core-3.2.5.RELEASE.jar
spring-expression-3.2.5.RELEASE.jar
spring-web-3.2.5.RELEASE.jar
spring-webmvc-3.2.5.RELEASE.jar
<servlet-class>org.springfrmaework.web.servlet.DispatcherServlet</servlet-class>
In above line there is spelling mistake. It should be
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

MVC using Spring hello world example

I am trying to implement the hello world example using Spring MVC but it is not giving the desired result.
This is my jsp page
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Print: ${message} world</h2>
</body>
</html>
HelloController.java
package com.sbv.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model)
{
model.addAttribute("message", "Hello");
return "index";
}
}
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" id="WebApp_ID" version="3.0">
<display-name>LoginSpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>/WEB-INF/jsp/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>
<servlet-name>Hello</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloServlet.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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.sbv.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
The output which I am getting is
Print: world
The ${message} is not getting printed
Someone please help me with this
Thanks in advance
The message attribute is added to the MODEL in the printHello method:
#Controller
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model)
{
model.addAttribute("message", "Hello");
return "index";
}
}
which will get executed when you make a GET request for /hello (http://host:port/appContext/hello). But you have the below entry in web.xml file:
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
and the web container will use this file for appending to a request for / to show index.jsp to the user, as you don't have any handler method mapped to / URL. So, in order to show the message to the user, remove the <welcome-file-list> entry from web.xml and change HelloController to this:
#Controller
#RequestMapping("/")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model)
{
model.addAttribute("message", "Hello");
return "index";
}
}
Get rid of
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
And make your request to
http://yourhost:yourport/YourApp/hello
to match your #Controller mapping.
If you make your request to
http://yourhost:yourport/YourApp
and any of the <welcome-file> entries exist, those will be chosen before your Servlet is hit.

Spring MVC model attribute value not printing

I just started learning spring mvc. Trying to follow a hello world sample.
Everything seems to be working fine, except the jsp view is not able to receive the model attribute value set in the controller.
Here's some code.
Controller:
package org.home.webapp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#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";
}
}
View
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<h1>Message : ${message}</h1>
</body>
</html>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.5//EN"
"http://java.sun.com/dtd/web-app_2_5.dtd" >
<web-app 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"
version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<display-name>Spring MVC Web 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>
</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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="org.home.webapp.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>
Using <%# page isELIgnored="false" %> helped.
We need to include org.springframework.web.servlet.ModelAndView instead of org.springframework.web.portlet.ModelAndView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldController {
#RequestMapping("/hello")
public ModelAndView hello()
{
System.out.println("Just Test iNside");
String message = "HELLO SPRING MVC HOW R U";
return new ModelAndView("hello", "message", message);
}
Using follwing code my issue resolved, may be its helpfull for you
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"> .....<web-app>
spring-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/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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">...</beans>
Also added follwing dependency
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
Try this changes may be it will work for you
you can use <th:block th:text="${your_attribut_name}"/>
example controller
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
view
<h1>Message : <th:block th:text="${message}"/></h1>
output
Message : Spring 3 MVC Hello World

Check box list not appearing in Spring MVC annotation

I am trying to implement dynamic check box list in Spring MVC with annotations. So, I tried this one out:
In home.jsp:
<c:forEach items="${categories}" var="cat">
<tr>
<td><form:checkbox path="catrgo" value="${category}"
label="${cat.categoryId}" /></td>
<td><c:out value="${cat.category}" /></td>
</tr>
</c:forEach>
And In HomeController:
#Controller
#RequestMapping(value = "/home")
public class HomeController {
#RequestMapping(method = RequestMethod.GET)
public String showForm() {
FeedDomain feedDomain = new FeedDomain();
System.out.println("Called");
CategoryService categoryService = new CategoryService();
try {
List<CategoryDomain> categoryDomainList = new ArrayList<CategoryDomain>();
CategoryDomain categoryDomain = new CategoryDomain();
categoryDomain.setCategory("aaa");
categoryDomain.setCategoryId(11111);
System.out.println("Size is " + categoryDomainList.size());
} catch (Exception exception) {
}
return "home";
}
Now I am going to home.jsp from index.jsp using tags but the problem is that showForm method is not getting called. What can be the reason?
I am posting below the complete flow:
Here is welcome.jsp from where I am calling home.jsp
<body>
<a href="home.jsp" class="btn btn-success btn-large disabled">Get
Us Feeds Now</a>
</body>
Now I have simplified home.jsp which is getting rendered:
<body>
<br />
<span class="badge badge-info"><h1>Chat Booster</h1></span>
<div class="leftHeader">
<h1>
<small>Subtext for header</small>
</h1>
</div>
<br></br>
<br></br>
</body>
And home page controller is :
#Controller
#RequestMapping(value = "/home")
public class HomeController {
#RequestMapping(method = RequestMethod.GET)
public String showForm() {
FeedDomain feedDomain = new FeedDomain();
// model.addAttribute("feedDomain", feedDomain);
System.out.println("Called");
CategoryService categoryService = new CategoryService();
return "home";
}
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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>HelloWorldExampleWithSpring3MVCInEclipse</display-name>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet- class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/app-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
app-config.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/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">
<context:component-scan base-package="com.controller" />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views
directory -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
My jsp pages are in webcontent folder only (not in web-inf). Now I will look for the main issue later but first point is showForm() method is not being called as sysouts are not priting anything at console. If I make home.jsp as default page of the application, then the method is called but not through the given approach. Can anyone comment on this?
Your link is incorrect, you need to call the controller action not the jsp:
<body>
<c:url var="homeLink" value="/home"/>
Get Us Feeds Now
</body>

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