Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request mapping - Spring 4 - doesn't work

I have a very simple problem. When in my .jsp files I have a link to **/registration the method viewRegistration is executed and everything is working fine. If I have a link to **/registration/getTags?tagName=blahblah page is not found. I have no idea why, because I think my requestMapping annotation looks correct... I would be very grateful for your help!

CONTROLLER:

@Controller
@RequestMapping(value = "/registration")
public class HelloController {

    final static Logger logger = Logger.getLogger(HelloController.class);

    @RequestMapping(method = RequestMethod.GET)
    public String viewRegistration(Map<String, Object> model, HttpSession session) {
        ...
   }

@RequestMapping(value = "/getTags", method = RequestMethod.GET)
    public @ResponseBody
    List<Tag> getTags(@RequestParam String tagName) {

        ....

    }
}

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" version="3.0">
  <display-name>aa</display-name>
  <servlet>
    <servlet-name>xxx</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>xxx</servlet-name>
    <url-pattern>/registration/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/xxx-servlet.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

xxx-servlet.xml :

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

    <context:component-scan base-package="movies.controller" />
    <context:property-placeholder location="/WEB-INF/properties/website.properties" />
    <context:component-scan base-package="com" />
    <context:annotation-config />
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"
        id="tilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tile/tilesJsp.xml</value>
            </list>
        </property>
    </bean>
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
        id="viewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.tiles3.TilesView" />
    </bean>


    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="website" />
    </bean>


</beans>

EDIT EDIT EDIT: I even tried sth more simple:

@RequestMapping(value = "/getTags")
    @ResponseBody
    public List<Tag> getTags() {
        String tagName="";
        return simulateSearchResult(tagName);

    }

but still /registration/getTags does not work..., page not found.

like image 306
user2455862 Avatar asked Dec 05 '22 04:12

user2455862


2 Answers

the servlet url mapping is as follows :

 <servlet-mapping>
    <servlet-name>xxx</servlet-name>
    <url-pattern>/registration/*</url-pattern>
  </servlet-mapping>

which means that all urls that begin with /registration/* will be handled by the servlet, what comes after it , it will be handled by the controller. So if you configure your controller with a @RequestMapping(value="/otherURL") it will serve the following Url :

http://localhost:xxxx/<appname>/registration/otherURL

And in this case in order to access the right method you should either:

  • @RequestMapping("/registration) from the controller, cause it is already mapped on the servlet level, and calling :

    http://localhost:xxxx/<appname>/registration/getTags?tagName=blahblah will work correctly. OR:

  • Call this URL : http://localhost:xxxx/<appname>/registration/registration/getTags?tagName=blahblah

like image 153
Rafik BELDI Avatar answered Dec 21 '22 02:12

Rafik BELDI


If you want to handle requests to **/registration/getTags, change your controller method mapping to

@RequestMapping(value = "**/registration/getTags", method = RequestMethod.GET)
like image 36
Predrag Maric Avatar answered Dec 21 '22 01:12

Predrag Maric