Request mapping - Spring 4 - not working

I have a very simple problem. When in my .jsp files there is a link to ** / registration, the viewRegistration method is executed and everything works fine. Should I have a link to ** / registration / getTags? TagName = blahblah page not found. I have no idea why, because I think my requestMapping annotation looks right ... I would really appreciate 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 easier:

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

    }

but still / checkin / getTags is not working ... page not found.

+4
source share
8 answers

URL- servlet :

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

, urls, /registration/*, servlet, , controller. , @RequestMapping(value="/otherURL"), Url:

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

:

  • @RequestMapping("/registration) , :

    http://localhost:xxxx/<appname>/registration/getTags?tagName=blahblah . :

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

+5

**/registration/getTags,

@RequestMapping(value = "**/registration/getTags", method = RequestMethod.GET)
+3

. , , "/registration/getTags?name=blahblah", "tagName". "/registration/getTags?tagName=blahblah".

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

+3

, - . , , , value @RequestParam, .

List<Tag> getTags(@RequestParam(value="name") String tagName)
+1

requestParam :

List<Tag> getTags(@RequestParam("tagName") String tagName)
+1

URL-

/registration/getTags/{blahblah}

-

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

}
+1

, ,

org.springframework.web.servlet.view.UrlBasedViewResolver

, jsp-. List, , , JSON http.

json, @JSONView

Spring @JsonView

viewResolver, org.springframework.web.servlet.view.json.MappingJacksonJsonView xml config, ,

+1

url /registration/ *

/registration

/getTags

So your url /registration/registration/getTags?name=blahblah

+1
source

Source: https://habr.com/ru/post/1569998/


All Articles