Spring MVC: How to get the right URL for controllers? - java

I'm a newbie to web development as well as Spring MVC. I'm testing on a silly example for the controllers, however, it seems that the controller has never been called, or at least it doesn't work as expected.
The goal is to click on a hyperlink in the "test.jsp" page and direct to "customers.jsp" page, with the controller being called, so that a message will be passed to the "customers.jsp" page.
Now I have the "customers.jsp" page showing correctly but with no transferred message displayed. The URL is http://localhost:8080/WebProject/view/customers.jsp.
Could you please help me find out the problem? I suppose it's because of the URL mapped to the controller. But is there a way that I can check the right URL to map for controllers? Thanks!
* updates at the end *
The project structure is like this:
--webapp
|--view
|--*.jsp
|--WEB-INF
|--*.xml
Here is my code:
test.jsp
<a href='<c:url value="customers.jsp"/>'>show customers</a>
or
show customers
both works properly.
customers.jsp
<h2>return: ${list}</h2>
Controller:
#Controller
#RequestMapping("/customers")
public class MenuController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView listCustomers() {
ModelAndView model = new ModelAndView("customers");
model.addObject("list", "controller: a list of customers");
return model;
}
}
and the configuration:
web.xml
<servlet>
<servlet-name>myDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
myDispatcher-servlet.xml
<context:component-scan base-package="com.example.spring.controller" />
<context:component-scan base-package="com.example.spring.model" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
UPDATE for those who might see this post later :
There is indeed a problem of url mapping as indicated by #javafan and #Beri. But after changing the url to show customers I still got Error 404 at http://localhost:8080/WebProject/customers. This is in fact caused by a package scanning error, so the Controllers cannot be found. To solve this, simply change the line in the "myDispatcher-servlet.xml" file:
<context:component-scan base-package="com.example.spring.controller" />
<context:component-scan base-package="com.example.spring.model" />
to
<context:component-scan base-package="controller" />
<context:component-scan base-package="model" />
Hope it helps :)

Javatan is right:
Try
<protocol>/<host>:<port>/WebProject/customers
Inside InternalResourceViewResolver bean definition you define prefix and suffix, those values are used to find jsp files in yur application. And have nothing in common with urls that your application has created.
Your url paths are defined inside controllers( not inside InternalResourceViewResolver).
So after application name you are using paths defined inside controllers.
You can also define #RequestMapping("/customers") on a method:)
Check if you have in your bean definition line, that looks like this:
<context:component-scan base-package="com.project.web.controllers"/>
This definition will scan for all classes inside com.project.web.controllers package (and subpackages) to find classes marked with #Controller annotation. You can check for more here:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html, chapters:
17.3.1 - how to register a controller
17.2 -how web.xml looks like
I hope this will help.
Ok, I have run local example with JSP:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>myDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value> <!-- Here you have your spring bean definition -->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Controller:
#Controller
public class MenuController {
#RequestMapping( value="/customers", method = RequestMethod.GET)
public ModelAndView listCustomers() {
ModelAndView model = new ModelAndView("customers");
model.addObject("list", "controller: a list of customers");
return model;
}
}
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.programcreek.helloworld.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

You can use this code to get the url of the server:
"show customers"
And you are mapping the Controller to /customers
so the url should be:
http://localhost:8080/WebProject/customers/customers.jsp

You are directly calling customer.jsp. Instead you have to call controller which in turn pass the request to customer.jsp. in HREF attribute, you need to mention value which is given in #RequestMapping annotation in your controller.
For more detailed example, visit my blog Here

Related

Spring MVC - Invalid view being executed (violating specified request mapping)

I have been trying to debug it for like hours and even tried to create the same project over and again. I don't know whether it's the issue due to IntelliJ IDEA or something else. I tried to google but couldn't find any solution. I am making a simple spring mvc demo application in intelliJ. A default structure was provided by intelliJ and a view "index.jsp" in web directory.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-mvc-demo-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">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
DebugController.java
package com.luv2code.springdemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Created by gaurav.ahirwar on 29/05/17.
*/
#Controller
public class DebugController {
#RequestMapping(value="/", method = RequestMethod.GET)
public String debug() {
return "hello";
}
}
The problem is that whenever I run the project and hit localhost:8080/ then index.jsp is executed but according to my configuration and mapping hello.jsp should be loaded. It may be possible that I am doing some silly mistake but I have tried as much as I can and now really frustrated. Please help me out. Thanks.
localhost:8080
Try this mapping:
#RequestMapping(value = {"", "/", "index.jsp"}, method = RequestMethod.GET)
I guess localhost:8080 equals "" path, but not /

Spring MVC handler mapping not working

I am very new to Spring MVC and have been following tutorials but they don't seem to be working.
I can load my index.jsp file fine but when I try to map it to the Dispatcher Servlet I keep getting resource not found errors.
HelloWorldController:
package com.javatpoint;
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 helloWorld() {
String message = "HELLO SPRING MVC";
return new ModelAndView("hellopage", "message", message);
}
}
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">
<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>*.html</url-pattern>
</servlet-mapping>
</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: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="com.javatpoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
my jsp files hellopage.jsp and welcomepage.jsp are in the folder jsp under my WEB-INF folder.
Index.jsp:
click|
click
I am trying to map hello.html to the HelloWorldController.java.
I would greatly appreciate any help as I am having problems mapping the first Spring MVC application.
The problem is with your servlet mapping, try changing it in your web.xml to:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
your url-pattern is set to *.html. Therefore only requests that ends with ".html" will be dispatched to spring mvc.
Add
<url-pattern>*.jsp</url-pattern>
to your servlet-mapping in web.js.
Can you try #RequestMapping(value="/hello",method = RequestMethod.GET ) ?
Your url pattern in the servlet-mapping is wrong.
You're saying go to the dispatcher servlet when the URL matches this:
*.html
So when you use "hello.html" the DispatcherServlet gets triggered.
However, there is no mapping for hello.html. There's only one for hello. HTML is static content, and it's not typical to serve that through a controller.
Try this:
Change the url-pattern to this:
<url-pattern>/</url-pattern>
Change the hello.html to hello.jsp
Change your method signature to return a string, and add the Model to the method.
#RequestMapping("/hello")
public String helloWorld(Model model) {
return "hello";
}
Your anchor tags should then point to the correct mapping. They'll look like this:
click|
So how does this work? These two pieces:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
The above is a pretty standard. In the servlet mapping, it's saying that anything that matches that URL pattern should hit the servlet named spring you defined in the web.xml.
The view resolver is looking in the /WEB-INF/jsp/ directory for files ending in .jsp. In other words, Spring will look for /WEB-INF/jsp/<string>.jsp. So when you return a String hello in the method, it is actually going to attempt to resolve /WEB-INF/jsp/hello.jsp.
If you understand the above, you may have realized you can serve *.html files this way also. They would require their own view resolver, however.

HelloWorld SpringMVC - HTTP Status 404

Im trying to create my first Spring MVC project but I keep getting error http status 404.
If i try to open an .jsp file outside of /views (So not using Spring MVC) it works fine
Here are my files:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>fj21-tarefas</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="br.com.caelum.tarefas" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Controller
package br.com.caelum.tarefas.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class OlaMundoController {
#RequestMapping("/olaMundoSpring")
public String execute(){
System.out.println("Executando a lógica com Spring MVC");
return "ok";
}
}
Libs
aopalliance-1.0.jar
commons-logging-1.2.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1-3.jar
jcl-over-slf4j-1.7.12.jar
joda-time-2.4.jar
log4j-over-slf4j-1.7.12.jar
slf4j-api-1.7.12.jar
slf4j-log4j12-1.7.12.jar
spring-aop-4.1.7.RELEASE.jar
spring-aspects-4.1.7.RELEASE.jar
spring-beans-4.1.7.RELEASE.jar
spring-context-4.1.7.RELEASE.jar
spring-context-support-4.1.7.RELEASE.jar
spring-core-4.1.7.RELEASE.jar
spring-expression-4.1.7.RELEASE.jar
spring-instrument-4.1.7.RELEASE.jar
spring-instrument-tomcat-4.1.7.RELEASE.jar
spring-jdbc-4.1.7.RELEASE.jar
spring-jms-4.1.7.RELEASE.jar
spring-messaging-4.1.7.RELEASE.jar
spring-orm-4.1.7.RELEASE.jar
spring-oxm-4.1.7.RELEASE.jar
spring-test-4.1.7.RELEASE.jar
spring-tx-4.1.7.RELEASE.jar
spring-web-4.1.7.RELEASE.jar
spring-webmvc-4.1.7.RELEASE.jar
spring-webmvc-portlet-4.1.7.RELEASE.jar
spring-websocket-4.1.7.RELEASE.jar
When your controller method returns a String, the String to be returned is the name of your view (jsp, template, etc).
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
According to the setup above (provided by you), Spring in intended to return a view located on /WEB-INF/views and the name of the view must has the suffix ".jsp".
Please, make sure the file "ok.jsp" is located inside this path, otherwise, Spring will not be able to load it.
In case "ok.jsp" is already located in this folder, please, provide us full stacktrace of the exception you are facing.
Yes, the view "ok.jsp" is located on the corrected folder.
I ended up fixing the issue by deleting all the libs and adding the dependency using maven.
If you are using spring, you should us spring boot to get started with any new service. Takes care of your basic dep tree. Refer to the tutorial at - http://projects.spring.io/spring-boot/#quick-start
Pretty straight forward.

Spring MVC No handler found

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/>

404 Error: "The requested resource /Controller is not available"

I have setup Spring MVC 3.0 & Hibernate on Apache Tomcat and got the application to launch without any errors.
However I’m available to route requests from my (welcome file) redirect.jsp to the home controller (/Home).
This is what is supposed to happen:
Welcome file redirect.jsp sends request using <%response.sendRedirect(/Home)%>
My home controller (/home) returns the view index which is in WEB-INF/views
This is my web.xml: I mapped the home controller (/Home) via annotation and inweb.xml however its still not being found.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<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>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Home</servlet-name>
<servlet-class>com.app.controller.spring.HomeController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Home</servlet-name>
<url-pattern>/Home</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>
This is my application-context.xml's snippet:
<!--bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />-->
<!-- 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.yourmarketnet.mvc" />
<!-- 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 />
<!-- mapping of static resources-->
<mvc:resources mapping="/resources/**" location="/resources/" />
<import resource="hibernate-context.xml" /
>
This is my app-servlet:
<?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"/>
<!--
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>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" p:viewClass="org.springframework.web.servlet.view.JstlView" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
However I’m getting a 404 Error,
The requested resource (/Home) is not available.
What I would actually like to do is to remove the redirect.jsp, have the application go to \Home controller on launch/startup, and the \Home controller returns the index view or any other view.
The first thing I noticed is that in your web.xml, you call your config location applicationContext.xml, but you later described it as application-context.xml. Make sure it is actually named applicationContext.xml. Also, you can remove the "Home" servlet from your web.xml; this will be handled by Spring through your dispatcherServlet. Finally, in your SimpleUrlHandlerMapping, you define the mapping to be index.html, but you need to also define Home if you want requests to Home to be handled by indexController. Hope that helps.

Categories