Search code examples
javajspstruts-1servlets

Does ServletRequest.setAttribute permit key names with periods?


I have a java webapp with a Struts 1 action that has the following code:

request.setAttribute("cat.sound", "meow");

On my jsp page, I have the following tag:

<c:out value="${cat.sound}" />

However, "meow" is never printed on the JSP page. This might work if I had an object of type "cat" to do something like:

request.setAttribute("cat", cat);

Unfortunately, this webapp does not have any object defined for the cats and the jsp pages are frozen (no changes allowed).

So is it possible to use request.setAttribute with a key name containing periods/dots? How would the JSP page need to reference the set parameter?


Solution

  • You can avoid having to create a Cat class if you set cat to a map with a String key "sound":

    request.setAttribute("cat", Collections.singletonMap("sound", "meow"));
    

    Collections#singletonMap() gives you a nice, succinct way to create a map with one entry.