3

I am trying to pass an int to my spring controller and from what I've read I should be using an @PathVariable to capture it in the controller. But none of what I have tried so far has worked. I just get a 404 error and spring doesn't even log that it failed.

Here is my controller code

@RequestMapping(value = "common/taskSummary/{taskId}")
public ModelAndView taskSummary(@PathVariable int taskId) {
    // Get user from Spring security
    User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    ModelAndView modelAndView = new ModelAndView("safeSiteHome");
    modelAndView.addObject("username", user.getUsername());
    return modelAndView;
}   

I have also tried mapping it with a "common/taskSummary.do/{taskId} but this does not work either.

But mapping without any path variable with "common/taskSummary.do" does work.

Here is my web.xml

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

<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/applicationContext.xml
        /WEB-INF/spring-security.xml  
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>           
<resource-ref>
    <description>ArcFlashMap DB Connection</description>
    <res-ref-name>jdbc/AFM_DB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>    
<servlet>
    <servlet-name>LoadResourcesServlet</servlet-name>
    <servlet-class>ie.premiumpower.services.reports.common.LoadResourcesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<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>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.json</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

Found out the problem. My web.xml had only being mapping ".do" urls to spring so I had to add .do to the @RequestMapping url "common/taskSummary/{taskId}.do" .

Or I could change the web.xml to map "/" to the spring dispatcher.

Thanks

8
  • Try this..@PathVariable("taskId") int taskId Commented Mar 26, 2015 at 14:59
  • I have actually tried this as well, doesn't work. Commented Mar 26, 2015 at 15:00
  • Could you share your web.xml? Commented Mar 26, 2015 at 15:18
  • 1
    I guess your dispatcher is mapped to *.do. If that's the case the url pattern that will resolve is common/taskSummary/{taskId}.do Commented Mar 26, 2015 at 15:21
  • 1
    @OneTwo Try mapping it as /. This would make dispatcher servlet as default servlet. You know your application better, so make sure it does not affect other flows. Commented Mar 26, 2015 at 15:29

1 Answer 1

3

The issue is your dispatcher mapped to *.do. So the URL pattern has to end with .do for it to handle.

With that said, common/taskSummary/{taskId}.do will resolve for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.