I've been having a little trouble with View Resolution under Spring 2.5.5. Basically I'm just trying to show my view with a message from the controller passed in. The issue comes when the Controller returns the ModelAndView but the DispatcherServelt says it can't find a Handler.
All the files seem to be in the correct place. I think the issue is that Spring can't resolve the view. From what I've seen I'm using the InternalResourceResolver correctly. I'm just at a loss as to why it is failing.
Once I've made a request this is whats in the catalina.out log:
Feb 8, 2010 3:27:24 PM com.madebymn.newsExample.web.IndexController handleRequest
INFO: Handling a Request: /index.jsp
Feb 8, 2010 3:27:24 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/newsExample/WEB-INF/jsp/index.jsp] in DispatcherServlet with name 'catchAll'
Here's my web.xml:
<web-app 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>A News Example</display-name>
<description>A News Example</description>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-jdbc.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>catchAll</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>catchAll</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>jsp/index.jsp</welcome-file>
</welcome-file-list>
Here's the 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="indexController" class="com.madebymn.newsExample.web.IndexController" />
<bean name="authorController" class="com.madebymn.newsExample.web.AuthorController">
<constructor-arg>
<ref bean="authorService" />
</constructor-arg>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index.jsp">indexController</prop>
<prop key="/author/*">authorController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
And here's my IndexController Class:
public class IndexController implements org.springframework.web.servlet.mvc.Controller
{
protected final Log logger = LogFactory.getLog(getClass());
#Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
logger.info("Handling a Request: " + request.getServletPath());
ModelAndView modelAndView = new ModelAndView("index");
modelAndView.addObject("message", "someMessage");
return modelAndView;
}
}
The problem is that you mapped your DispatcherServlet as *.jsp, when views are JSPs too. Try to map DispatcherServlet to something different, like *.html
Related
My application doesn't work with requestmapping, the request always works with the same method handleRequest, the controller is RestController.java and the URLs redirects to it when has the path /REST2/*, it works well but always with the same method. But the method "update" never works when I send the request to locahost:9080/myapp/REST2/rrr and if I change the URL i.e. localhost:9080/myapp/REST2/XXX also the request is going to handleRequest method.
The RequestMapping doesn't work well, I tried with other solutions but doesn't work..
Do you have any idea?
Restcontroller.java
public class RestController implements Controller{
private Configuration config;
private static String[] requestHeaders = {"accept", "pragma"};
private static String[] responseHeaders = {};
public void setConfig(Configuration config)
{
this.config = config;
}
#RequestMapping(value="/rrr") public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception
{
String asset_name = request.getParameter("assetid");
response.setContentType("text/plain"); // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
String values = "Working"+ asset_name;
response.setContentLength(values.length());
PrintWriter out = response.getWriter();
out.println(values);
return null;
}
#RequestMapping(value ="/home")public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
Arrays.sort( requestHeaders );
Arrays.sort( responseHeaders );
String values;
String asset_name = request.getParameter("assetid");
...
response.setContentLength(values.length());
//
PrintWriter out = response.getWriter();
out.println(values);
return null;
}}
spring-servlet.xml
<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" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- View resolver. Prepends prefix /WEB-INF/jsp and suffix .jsp to view names. -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Always use full path when mapping URLs to bean names. -->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
</bean>
<!-- Home page. -->
<bean name="/home.app" class="com.fatwire.wem.sample.HomeController">
<property name="config" ref="config" />
</bean>
<!-- Installation page. -->
<bean name="/install.app" class="com.fatwire.wem.sample.InstallController">
<property name="config" ref="config" />
</bean>
<!-- Layout page. -->
<bean name="/layout.app" class="com.fatwire.wem.sample.LayoutController" />
<!-- REST proxy page. -->
<bean name="/REST/**" class="com.fatwire.wem.sample.ProxyController">
<property name="config" ref="config" />
</bean>
<!-- REST proxy page. -->
<bean name="/REST2/**" class="com.fatwire.wem.sample.RestController">
<property name="config" ref="config" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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" >
<!-- Bootstrap Spring configuration to be used by both SSO and MVC -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- WEM SSO Listener -->
<listener>
<listener-class>com.fatwire.wem.sso.SSOListener</listener-class>
</listener>
<!-- Configure Spring MVC -->
<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>*.app</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/REST/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/REST2/*</url-pattern>
</servlet-mapping>
<!-- WEM SOO Filter -->
<filter>
<filter-name>WEM SSO Filter</filter-name>
<filter-class>com.fatwire.wem.sso.SSOFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>WEM SSO Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Request AJAX
function load_form(data){
var asset_id = data;
$.post('REST2/home', {
assetid : data,
}, function(responseText) {
alert(responseText);
});
}
function update_form(){
var asset_id = "XXX";
$.post('REST2/rrr', {
assetid : asset_id,
}, function(responseText) {
alert(responseText)
});
Try to add #Controller and #RequestMapping annotations to RestController classlike this:
#Controller
#RequestMapping(value="/REST2")
public class RestController implements Controller{
And add method=RequestMethod.POST the #RequestMapping of methods like this:
#RequestMapping(value ="/home", method=RequestMethod.POST)
...
#RequestMapping(value ="/rrr", method=RequestMethod.POST)
I have been working with Google App Engine for last 3 years but never faced any such issue before. I have created a project using Spring-4 and Objectify.
The issue I am facing is that on local all URLs are working fine, but when I am deploying the same to the appspot I am getting 404 errors on the URLs which needs to return the JSP pages, although the URL which should return the JSON or XML response those URL are working fine.
Appengine logs are also not providing any relevant information. I cannot understand just one thing that if it running fine locally why is it not working on appspot.
Following are my configuration files.
appengine-web.xml
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>swissinfos</application>
<version>2</version>
<threadsafe>true</threadsafe>
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
<sessions-enabled>true</sessions-enabled>
</appengine-web-app>
applicationContext.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">
<!-- 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="com.lumin.mytalk." />
<!-- 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 />
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"></property>
</bean>
</mvc:interceptors>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/common.properties</value>
</list>
</property>
</bean>
<bean id="converter" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.lumin.mytalk.common.util.DozerConverter">
<property name="mapper">
<bean class="org.dozer.spring.DozerBeanMapperFactoryBean">
<property name="mappingFiles">
<value>/WEB-INF/classes/dozer/dozer-bean-mappings.xml</value>
</property>
</bean>
</property>
</bean>
</list>
</property>
</bean>
</beans>
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.lumin.mytalk." />
<!-- Application Message Bundle -->
<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<filter>
<filter-name>ObjectifyFilter</filter-name>
<filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ObjectifyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<error-page>
<error-code>404</error-code>
<location>/jsp/error.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>jsp/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet
</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value/>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>
</web-app>
API Controller Sample
#Controller
public class CategoryController extends AbstractGenericController{
#Autowired
private CategoryService categoryService;
#ResponseBody
#RequestMapping(value = Path.Url.API + Path.Url.CATEGORY + Path.OperationUrl.CREATE, method = RequestMethod.POST, produces = {"application/json", "application/xml" })
public Object create(#RequestBody Category category) {
Category isExisting = categoryService.getCategoryByName(category.getCategoryName());
if (isExisting == null) {
Long id = categoryService.createWithID(category);
return new Response(null, STATUS.SUCCESS, "Category created successfully. Id :" + id);
}else{
return new Response(null, STATUS.FAILURE, "Category already exist. Please use update category function.");
}
}
}
Web Controller Sample
#Controller
public class LoginLogoutController {
#RequestMapping(value = Path.Url.LOGIN)
public String loginPage(#RequestParam(value = "error", required = false) boolean error, #RequestParam(value = "maidID", required = false) String maidID, ModelMap map) {
if (error == true) {
map.put("error", "You have entered an invalid username or password!");
}if( maidID != null && !maidID.equals("")){
map.addAttribute("maidID", maidID);
}
if((error == true) && (maidID != null && !maidID.equals(""))){
map.put("error", "You have entered an invalid username or password!");
map.addAttribute("maidID", maidID);
}
return Path.Jsp.LOGIN_PAGE;
}
}
GAE Log Entry
2014-09-04 13:08:27.302 / 404 44ms 0kb Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0 module=default version=2
123.63.241.35 - - [04/Sep/2014:00:38:27 -0700] "GET / HTTP/1.1" 404 256 - "Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0" "swissinfos.appspot.com" ms=44 cpu_ms=87 cpm_usd=0.000029 app_engine_release=1.9.10 instance=00c61b117c9c5e35c6e16583e442bfebd62dc8f0
<?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>SpringMVCTutorial</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>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- <url-pattern>*.html</url-pattern> -->
<url-pattern>/</url-pattern>
<!-- <url-pattern>*.htm</url-pattern> -->
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>
</web-app>
this is the file 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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<!-- <mvc:annotation-driven />
<mvc:view-controller path=”/index.htm” view-name=”index”/> -->
<!-- <context:component-scan base-package="controller"/> -->
<bean name="/helloworld.htm" class="controller.HelloWorldController"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="0"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
<!-- <prop key="helloworld.htm">helloworld</prop> -->
</props>
</property>
</bean>
</beans>
and this is my controller
package controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;
#Controller
public class HelloWorldController {
protected final Log logger = LogFactory.getLog(getClass());
#RequestMapping(value="/helloworld", method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info("Return View");
return new ModelAndView("helloworld");
}
}
Can anyone help me to find the reason of this error i already tried multiple solutions as they have the same error but none worked for me its the first time i do Spring from scratch :(..
WARNING: No mapping found for HTTP request with URI [/SpringMVCTutorial/] in DispatcherServlet with name 'dispatcher'.......
Followed by error 404 on the browser.
I think you have two problems here:
First, you need to use <mvc:annotation-driven /> to enable annotation based controller.
Second, it seems to me that you want to have a default page when typing the context root only.
In this case, you need to add mapping like this:
<mvc:view-controller path="/" view-name="index.htm"/>
Or change <prop key="index.htm">indexController</prop> to
<prop key="/">indexController</prop>
I'm newbie to spring mvc and trying to develop a very basic login webapp. I get the below error while running the project. I have tried almost everything and could not fix this error since last two weeks. Please can someone help me.
May 21, 2013 2:37:12 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringWeb/WEB-INF/jsp/loginnn.jsp] in DispatcherServlet with name 'spring'
My jsp pages resides under WEB-INF/jsp. The method loginpage in my controller is gettting invoked but the view name is not being rendered and resolved. Greatly appreciate your help.
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>testspring</display-name>
<servlet>
<servlet-name>frontcontrol</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>frontcontrol</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
frontcontrol-servlet.xml
<context:component-scan base-package ="com.shell.spring.testspringapp">
</context:component-scan>
<bean id ="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Controller class
#Controller
public class Firstcontrol {
#RequestMapping(value="/")
public ModelAndView invokeme(Model m)
{
ModelAndView mav=new ModelAndView();
mav.setViewName("result");
System.out.println("In method");
return mav;
}
#RequestMapping(value="/submit" ,method=RequestMethod.GET)
public String submit(Model m)
{
System.out.println("In submitmethod");
return "submit";
}
}
Since you're mapping your DispatcherServlet to '/' try adding <mvc:default-servlet-handler/> to your spring-servlet.xml
I saw same error, turned out that you need enable MVC annotations separately
<mvc:annotation-driven/>
In addition to
<context:Annotation-config/>
<context:component-scan base-package="your base packege " />
You should check the following section in spring configuration xml. Maybe you copied it from somewhere and forgot to make it your package name. Spring will not be able to scan packages if it so and eventually show this error.
In the web.xml file the configuration file (spring-servler.xml) should be specified to be used by the dispatcher servlet, since your not using the conventional name, which is the [servlet-name]-context.xml.
Since Spring cannot find the configuration file for the dispatcher servlet the viewresolver is never registered. I assume that 'spring-servler.xml is located within the WEB-INF folder in my example, so you may need to adjust.
Also notice I switched the servlet mapping to / which helps if you need to resolve static resources, since / acts as a catch all as opposed to mapping everything through the dispatcher.
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-servler.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
You could also try using a regular view resolver, if your not using JSTL.
Replace the jstl view resolver:
<bean id ="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
With:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Try returning a String from your controller that specifies the view name:
#RequestMapping(value="/")
public String loginpage(Model model)
{
Employee emp=new Employee();
ModelAndView mav=new ModelAndView();
model.addObject("emp", emp);
return "loginnn";
}
#sanjay and #Will Keeling are both right. It is required to use <mvc:default-servlet-handler/> (as the last handler) and enable both <mvc:annotation-driven/> and <context:Annotation-config/>
I'm building a Web application with Java. Trying to integrate Spring Web MVC 3.0 framework. But I can't even force simply to show the page. I am using NetBeans 7.0 and Tomcat 6.0.
This is what I've got:
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
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: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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="forum.web" />
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<!-- org.springframework.web.servlet.view.InternalResourceViewResolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
Controller:
package forum.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
*
* #author
*/
#Controller
public class RegistrationController
{
#RequestMapping("/registration.htm")
public ModelAndView registrationWindow()
{
return new ModelAndView("registration");
}
}
XHTML page:
<tr id="log_reg">
<td>
Log in
</td>
<td></td>
<td>
Register
</td>
</tr>
Running this command I see index page:
http://localhost:8088/Forum/index.htm
On the page I click the link, what suppose to open registration page. But instead is I see HTTP 404 error.
The link:
http://localhost:8088/Forum/registration.htm
Found errors on NetBeans:
18-Jun-2011 14:45:19 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/index.htm] onto handler [org.springframework.web.servlet.mvc.ParameterizableViewController#1fb0fc2]
18-Jun-2011 14:45:20 org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 1279 ms
18-Jun-2011 14:45:20 org.apache.catalina.core.StandardContext start
INFO: Container org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/Forum] has already been started
18-Jun-2011 14:45:35 org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/Forum/registration.htm] in DispatcherServlet with name 'dispatcher'
18-Jun-2011 14:51:53 org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/Forum/registration.htm] in DispatcherServlet with name 'dispatcher'
I can't release the cause of these errors. Code looks fine, in my opinion. I tried many things by this time.
Any help?
Best regards
Probably it's because you use both annotation mapping and SimpleUrlHandlerMapping.
Try to add bean:
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping">
</bean>
and add it as a handler to your urlMapping bean:
<property name="interceptors">
<list>
<ref bean="handlerMapping"/>
</list>
</property>
It's easier to use only one of them, especially annotation based controllers. At this case you have to throw out your urlMapping bean, and instead of indexController bean use simple class like:
#Controller
class PagesCtrl {
#RequestMapping("/index.htm")
ModelAndView index() {
ModelAndView mav = new ModelAndView("index")
return mav
}
}