views:

561

answers:

4

I want to display a specific message based on the URL request on a JSP.

the request URL can be:

/app/cars/{id}

OR

/app/people/{id}

On my messages.properties I've got:

events.action.cars=My car {0} event
events.action.people=My person {1} event

Finally, on my JSP page I want to have the following code:

<spring:message code="events.${element.cause}.${?????}"
                arguments="${element.param['0']},${element.param['1']}"/>

I need help figuring out which expression I could use to parse the request URL and obtain the word before the ID.

A: 
/app/(cars|people)/([^/]*)$

will put cars or people in backreference \1, depending on the match, and whatever is left right of the last slash in backreference \2.

Tim Pietzcker
JSTL doesn't have any regex flavored functions.
BalusC
+2  A: 

You can access the request URI in JSTL (actually: EL) as follows:

${pageContext.request.requestURI}

(which thus returns HttpServletRequest#getRequestURI())

Then, to determine it, you'll have to play a bit round with JSTL functions taglib. It offers several string manipulation methods like split(), indexOf(), substringAfter(), etc. No, no one supports regex. Just parse it.

Kickoff example:

<c:set var="pathinfo" value="${fn:split(pageContext.request.requestURI, '/')}" />
<c:set var="id" value="${pathinfo[pathinfo.length - 1]}" />

And use it as ${id}.

BalusC
I think the asker wants to know how to do it in Springs.
The Elite Gentleman
@The Elite Gentleman: JSTL is been explicitly tagged and mentioned. You're welcome to post a Spring targeted answer though. If it can be done in Spring :)
BalusC
Sorry, I haven't seen the tag... +1 for that and your answer :-)
The Elite Gentleman
@The Elite Gentleman: I think you forgot to click upvote :o
BalusC
Done... :-) (stupid 15 characters length comment)
The Elite Gentleman
A: 

My solution so far is to have a RequestUtils class that match the regex ".?/jsp/(\w+)/..jsp" and return the group(1).

in my Jsp I got:


<% request.setAttribute("entity", RequestUtils.getEntityURI(request)); %>
<spring:message code="events.${element.cause}.${entity}"
                arguments="${element.param['0']},${element.param['1']}"/>

this of course did the trick. But still it would be better not to have any Java code within the JSP.

JorgeO
A: 

If I understand you correctly, I think you need to do something like this:

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
  model.addAttribute("ownerId", ownerId);  
  return "myview"; 
}

As you can see, here the ownerId is read from the URL by Spring MVC. After that, you simply put the variable in the Model map so you can use it in your JSP.

Hans Westerbeek
not really in spring I got a controller to display a car or a person. On each of those view a <c:import url="/includes/fragments/event.jsp"/> the spring message is formatted to be different for each entity. like events.activate.cars=car activated and events.activate.people=Person activated.
JorgeO