Announcement Announcement Module
Collapse
No announcement yet.
@RequestMapping pointing to "/" Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @RequestMapping pointing to "/"

    Hello,
    I appologize if this was asked before, but @RequestMapping term is very popular and it is very hard to find what I'm looking for on the forum - possibly I can't even name it

    In my Spring MVC project I've got a controller, where I've got the following method (no annotations except @Controller on the class):
    Code:
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model)
    {
    	return "home";
    }
    Unfortunately this won't get resolved correctly, when I type in my browser: http://localhost:8080/website/ (I get HTTP Status 404).

    If I use:
    Code:
    @RequestMapping(value = "/home-page", method = RequestMethod.GET)
    public String home(Locale locale, Model model)
    {
    	return "home";
    }
    then if I browse to http://localhost:8080/website/home-page I'm good...

    Now, if I make the following change (remove: value = "/")
    Code:
    @RequestMapping(method = RequestMethod.GET)
    public String home(Locale locale, Model model)
    {
    	return "home";
    }
    there's no more 404 error, but the page is broken, because all the links are wrong. E.g.
    HTML Code:
    <link href="/website/resources/css/styles.css;jsessionid=EA5D03D0894A03CF5DEF915402DA9359" rel="stylesheet" type="text/css" />
    instead of
    HTML Code:
    <link href="/resources/css/styles.css;jsessionid=EA5D03D0894A03CF5DEF915402DA9359" rel="stylesheet" type="text/css" />
    What am I doing wrong in mapping by using @RequestMapping?

    To be complete I attach my web.xml and application context:
    HTML Code:
    <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" metadata-complete="false">
    
    	<!-- 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>
    HTML Code:
    <?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    		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">
    
    	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    
    	<!-- Enables the Spring MVC @Controller programming model -->
    	<mvc:annotation-driven />
    
    	<!-- Scan for annotation based controllers -->
    	<context:component-scan base-package="controllers" />
    
    	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    	<mvc:resources mapping="/resources/**" location="/resources/" />
    
    	<!-- Configure Apache Tiles for the view -->
    	<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    		<property name="definitions">
    			<list>
    				<value>/WEB-INF/jsp/layouts/layouts.xml</value>
    				<value>/WEB-INF/jsp/controllers/views.xml</value>
    			</list>
    		</property>
    	</bean>
    
    	<!-- Apache Tiles view resolver -->
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    		<property name="requestContextAttribute" value="requestContext"/>
    		<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
    	</bean>
    
    	<!-- bean responsible for loading messages*.properties files for i18n internationalisation -->
    	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    		<property name="basename" value="classpath:messages" />
    		<property name="defaultEncoding" value="UTF-8" />
    	</bean>
    
    	<!-- beans helping to change current locale for i18n internationalisation -->
    	<!-- i18n 1.: change listener -->
    	<mvc:interceptors>
    		<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?lang=de -->
    		<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    			<property name="paramName" value="lang" />
    		</bean>
    	</mvc:interceptors>
    	<!-- i18n 2.: saves and reads cookies, where current (changed) locale is stored -->
    	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    		<property name="defaultLocale" value="en" />
    	</bean>
    
    </beans>
Working...
X