0

I need to add this to two or more JSP pages. I want to try to avoid boilerplate code and need this code snippet in several JSP pages.

  1. where do I place this code snippet as far as a directory com.lizardking.demographic.xxx?
  2. how would I get it to be placed in the JSP file coming from another external file?
  3. what would this file be considered as far as naming convention, for example, a utility class, etc.

I'm using Spring Boot for the backend.

<%
    ArrayList<String> maritalStatusList = new ArrayList<String>();

        maritalStatusList.add("single/soltero");
        maritalStatusList.add("married/casado");
        maritalStatusList.add("divorced/divorciado");
        maritalStatusList.add("widow(er)/viudo(a)");

    request.setAttribute("maritalStatusList", maritalStatusList);
%>


Marital status/Estado civil
<select id="maritalStatus" name="maritalStatus">
<option value=""></option>
<c:forEach items="${maritalStatusList}" var="listItem">
    <option value="${listItem}" <c:if test="${listItem eq demoEntity.maritalStatus}">selected="selected"</c:if> >
        ${listItem}
    </option>
</c:forEach>    
</select>
coder3
  • 357
  • 1
  • 3
  • 14
  • Please check this once https://stackoverflow.com/questions/6659986/how-to-provide-string-constants-for-drop-down-lists-in-jsp – Sariq Shaikh Mar 03 '20 at 07:12
  • Does this answer your question? [how to provide string constants for drop down lists in jsp](https://stackoverflow.com/questions/6659986/how-to-provide-string-constants-for-drop-down-lists-in-jsp) – Sariq Shaikh Mar 03 '20 at 07:13
  • @SariqShaikh I implemented what was on that page but it did not work for me. With or without the CDI version. What I ended up doing is placing the Map in my Controller – coder3 Mar 04 '20 at 03:32
  • Spring is not standard JEE. In Spring you have `@Singleton` instead of `@ApplicationScoped`. – BalusC Mar 04 '20 at 07:40

2 Answers2

1

If you are using Spring Boot MVC, maybe it's better to insert maritalStatusList from the controller into the Model object. So you can use a method to generate the data of your ArrayList, let's say generateStatusList(), and from your Controller method, before returning the .jsp:

...
ArrayList <String> maritalStatusList = generateStatusList();
model.addAttribute("maritalStatusList", maritalStatusList);
...

And then you can access maritalStatusList with your .jsp code

Take a look at https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-modelattrib-methods

JoM
  • 236
  • 2
  • 4
  • @coder3 Yes, you are right, the answer that you have posted below is this approach. – JoM Mar 04 '20 at 14:59
  • @coder3 By the way, I don't feel it bloats the controller... it's my opinion but I have used this approach several times and it works fine for me... maybe the method is not directly implemented in the Controller, but in another class. – JoM Mar 04 '20 at 15:07
0

What I ended up doing is placing this method in my controller. And then sending it to the view via a ModelMap. I'm not sure if it's the "right" way to do it. It seems to bloat the controller.

public Map<String, String> getMaritalStatusList(){

   Map<String, String> maritalStatusList = new HashMap<String, String>(); 

   maritalStatusList.put("married/casado", "married/casado");
   maritalStatusList.put("married/casado","married/casado");
   maritalStatusList.put("divorced/divorciado", "divorced/divorciado");
   maritalStatusList.put("widow(er)/viudo(a)", "widow(er)/viudo(a)");

   return maritalStatusList;
}
coder3
  • 357
  • 1
  • 3
  • 14