6

Is it possible to: set a URI template in the mvc:view-controller element of the *-servlet.xml file or in a controller method and then use/get that path parameter in a jsp?

I understand that using @PathVariable in a controller method will give me access to the path parameter in that controller method. But how can I gain access to the path parameter in the jsp?

For example, is it possible to do something like:

*-servlet.xml file:

<beans...>
<mvc:view-controller path="/home" view-name="home"/>
<mvc:view-controller path="/home/{error}" view-name="home"/>
</beans>

jsp file:

<c:if test="${not empty param['error']}">
<span class="error">You have an error...</span>
</c:if>

2 Answers 2

16

If you want access to it in the jsp, return it as an attribute from the controller:

@RequestMapping("/home/{error}")
public void handleError(@PathVariable String error, ModelMap model) {
    //your regular stuff

    model.addAttribute("error", error);
}

-

<c:if test="${not empty error}">
<span class="error">You have an error...</span>
</c:if>
Sign up to request clarification or add additional context in comments.

2 Comments

Note the wrong EL syntax, ${} around error is not needed.
what if there is no controller? the setup i have is just the map between the request and the page(view) how would you treat parameterized url in such a situation. thanks
1

The Map of resolved path variables is exposed as an attirubte with name specifed by HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE. So, the solution is

<c:if test="${not empty requestScope['org.springframework.web.servlet.HandlerMapping.uriTemplateVariables']['error']}">

But accessing it this way is probably not a best idea, Affe's solution is cleaner.

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.