JSTL Analysis Request URL

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

The request URL may be:

/app/cars/{id}

OR

/app/people/{id}

On mine messages.propertiesI have:

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 can use to parse the request URL and get the word before the identifier.

+3
source share
4 answers

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

${pageContext.request.requestURI}

(which thus returns HttpServletRequest#getRequestURI())

, , JSTL function taglib. , split(), indexOf(), substringAfter() .. , . .

Kickoff:

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

${id}.

+7
/app/(cars|people)/([^/]*)$

cars people backreference \1, , , backreference \2.

0

, RequestUtils, ".?/jsp/(\ w +)/.. jsp" (1).

Jsp :


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

, , . Java- JSP.

0

, , - :

@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 ownerId is read from the Spring MVC URL. After that, you simply put the variable in the model map so that you can use it in your JSP.

0
source

Source: https://habr.com/ru/post/1735749/


All Articles