Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC trying to pass a variable by url. @PathVariable

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

like image 517
OneTwo Avatar asked Dec 01 '25 16:12

OneTwo


1 Answers

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.

like image 76
minion Avatar answered Dec 03 '25 07:12

minion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!