Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3.0.6 MVC @PathVariable and @RequestParam blank/empty in JSP view

I have been trying to get an incredibly simple controller/view set up, and just can't make it work. In my web.xml, I've defined a <servlet> called servlet-context.xml, which is running ok. In servlet-context.xml, I've set:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"

<...other stuff in here... />

<mvc:annotation-driven />

among other things. My understanding is this is all that's needed to use @ annotations.

In my controller, I have:

@RequestMapping(value="/student/{username}/", method=RequestMethod.GET)
public String adminStudent(@PathVariable String username, @RequestParam String studentid) {
    return "student";
}

And in my student.jsp view, I have:

<p>This is the page where you would edit the stuff for ${username}.</p>
<p>The URL parameter <code>studentid</code> is set to ${studentid}.</p>

When I make a request to http://localhost:8080/application/student/xyz123/?studentid=456, I get the view I expect, but all the variables are blank or null:

<p>This is the page where you would edit the stuff for .</p>
<p>The URL parameter <code>studentid</code> is set to .</p>

I suspect it's a problem with the way my web.xml or servlet-context.xml are set up, but I can't find the culprit anywhere. There's nothing showing up in any logs as far as I can see.


Update: I was basing my code off this part of the spring-mvc-showcase:

@RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)
public String pathVars(@PathVariable String foo, @PathVariable String fruit) {
    // No need to add @PathVariables "foo" and "fruit" to the model
    // They will be merged in the model before rendering
    return "views/html";
}

...which works fine for me. I can't understand why this example works but mine doesn't. Is it because they're doing something different with servlet-context.xml?

<annotation-driven conversion-service="conversionService">
    <argument-resolvers>
        <beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/>
    </argument-resolvers>
</annotation-driven>
like image 663
alexmuller Avatar asked Nov 27 '11 16:11

alexmuller


People also ask

What is difference between @PathVariable and @RequestParam in Spring?

Difference between @PathVariable and @RequestParam in Spring 1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.

What does @PathVariable do in Spring MVC?

@PathVariable is a Spring annotation which indicates that a method parameter should be bound to a URI template variable. If the method parameter is Map<String, String> then the map is populated with all path variable names and values. It has the following optional elements: name - name of the path variable to bind to.

Can we use RequestParam and PathVariable together?

The @PathVariable annotation is used for data passed in the URI (e.g. RESTful web services) while @RequestParam is used to extract the data found in query parameters. These annotations can be mixed together inside the same controller. @PathParam is a JAX-RS annotation that is equivalent to @PathVariable in Spring.

What are the differences between @RequestParam and @PathVariable annotations?

2) @RequestParam is more useful on a traditional web application where data is mostly passed in the query parameters while @PathVariable is more suitable for RESTful web services where URL contains values.


2 Answers

Create a Model map and add those parameter name/value pairs to it:

@RequestMapping(value="/student/{username}/", method=RequestMethod.GET)
public String adminStudent(@PathVariable String username, @RequestParam String studentid, Model model) {
    model.put("username", username);
    model.put("studentid", studentid);

    return "student";
}
like image 92
duffymo Avatar answered Nov 16 '22 02:11

duffymo


Aha! Figured it out, finally.

The spring-mvc-showcase is using Spring 3.1, which will automatically expose @PathVariables to the model, according to SPR-7543.

As pointed out by @duffymo and @JB Nizet, adding to the Model with model.put() is the thing to do for versions of Spring earlier than 3.1.

Ted Young pointed me in the right direction with Spring: Expose @PathVariables To The Model.

like image 29
alexmuller Avatar answered Nov 16 '22 03:11

alexmuller