Reading a model attribute value using Java in JSP

Viewed 101

I'm basically trying to assign my model attribute value to a Java string variable. I googled some things and I used this method below:

<input type="hidden" id="businessNameId" name="businessNameId" value="${business.business.name}">
    <% String businessName = request.getParameter("businessNameId");
       if(businessName.contains("- [")){ %>
             <p class="industry-title">Restaurant Location</p>
    <%}%>

I also tried the attribute method which is:

<% String businessName = (String) request.getAttribute("business.business.name");
   if((businessName).contains("- [")){ %>
     <p class="industry-title">${business.business.address.city}</p>
   <}%>

Still doesn't work.

Although this does not seem to work, basically the variable business Name is not being initialized. I need to use Java in this case.

1 Answers

If your value is in an attribute and not a parameter, then you can access it using:

<% String businessName = (String) request.getAttribute("businessNameId") %>

If this doesn't work, you should check that businessNameId is indeed in the parameters, for example using:

<%@ page import = "java.util.*" %>
<%
   Enumeration in = request.getParameterNames();
   while(in.hasMoreElements()) {
      String paramName = in.nextElement().toString();
      out.println(paramName + " = " + request.getParameter(paramName));
}
%>
Related