How do I display action data in a JSP? - java

I am using an <s:iterator> tag in my JSP to display a List of people objects.
I tried creating ListOfPersons as a List in the action class, along with a getter and setter. I am still unable to display the data--how can I do it?
<s:iterator value="ListOfpersons" status="stat">
When I tried printing the size of list I am getting zero.

If I understood you right, you are having problem with expressing list using OGNL on JSP pages. You should name fields with YourListName[index].property format. Then OGNL will understand that you have a list called YourListName and its element at index have property with value which is on its input.
Please see example below:
<table>
<s:iterator value="ListOfpersons" status="status">
<tr>
<td><s:textfield name="ListOfpersons[%{#status.index}].firstname"/></td>
<td><s:textfield name="ListOfpersons[%{#status.index}].lastname"/></td>
<td><s:textfield name="ListOfpersons[%{#status.index}].age"/></td>
<td><s:textfield name="ListOfpersons[%{#status.index}].sex"/></td>
</tr>
</s:iterator>
</table>

Short Answer: store the information in the request and access it in the jsp.
Longer Answer:
Create some objects in the servlet (in your case, the action).
Store the objects in some JSP scope (HttpServletRequest.setAttribute()).
Forward (dispatch) the request to the JSP page (this is just struts config, you are already doing this).
In the JSP page, reference the variables (perhaps using a c:out tag or just use an EL expression in the JSP page text).
Some code (struts 1.x):
class Blah extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
... do stuff
request.setAttribute("Blammy", "Blammy Value");
... return some ActionForward.
}
}
In a JSP:
<span>The value of the Blammy variable is this here thing: ${Blammy}</span>
or
<span>The value of the Blammy variable is this here thing: <c:out value="${Blammy}"/></span>
Once you have the basic concepts down, just set a request attribute with the List in question and access it using the iterator tag in your JSP.

Related

How to access a list item using its index in JSP

In a code that follows Spring MVC, I have 2 lists in my java code that I would like to use in my JSP view. I set them like this:
public ModelAndView circularListView(HttpServletRequest request, Principal principal, HttpSession session, Locale locale, ModelAndView mav, int startOffset) {
//some code
mav.addObject("circularsList", circularsList);
mav.addObject("documentNameList", documentNameList);
return mav;
}
Now I would like to iterate on both lists circularsList and documentNameList in a single for loop in the JSP page, but it seems that I can only set one variable name like this:
<c:forEach items="${circularsList}" var="circular" varStatus="status">
To access a value in the second list which is documentNameList, I do it like this:
<input type="hidden" id="circDocNam" value="${documentNameList[status.index]}"/>
Unfortunately, this does not seem to work, and the value in the above line is empty.
What to do?
In conclusion: How to access a list item using its index in JSP?
This worked for me:
<input type="hidden" id="circDocNam" value="<c:out value="${documentNameList[status.index]}"/>"/>

Why is my post not give data to my controller from my form?

I am developing a Sprin MVC application and I have a form containing a table in one of the UI jsp's, (welcome.jsp) and when the submit button is clicked, I am trying to print out the data in the form to the web applications console.From there i intend to parse the checkboxes that are selected and then have the controller send the 'selected' data back to the databased to be updated to the next status in the applications flow.
So far the form is 'successfully' posting as in no error or exceptions is being thrown, but the printed statement in the console is blank which makes me think that no data is being sent, and I would greatly welcome any help to fix this.
Here is the setup of what I have, not the actual code but a rough set up of the elements and methods.
welcome.jsp:
<form action="<c:url value="/postPage" />"method="post" modelAttribute="rTable">
<br/>
<table>
<thead>
<tr>
<th>title1</th>
<th>title2</th>
<th>title3</th>
<th><select>
<option>option1</option>
<option>option2</option>
</select></th>
</tr>
</thead>
<tbody>
<tr>
<td>value1</td>
<td>value1</td>
<td>value1</td>
<td><input type="checkbox" value="row_data_id" /></td>
</tr>
</tbody>
<tfoot>
<tr><td colspan="4"></td>
</tfoot>
</table>
<br/>
</form>
My controller has the following method in it with all the necessary libraries imported:
controller.java
#RequestMapping(value="/postPage", method = RequestMethod.POST)
public String processUpdate(#ModelAttribute("rTable") String table, ModelMap model) {
System.out.println(table);
return "postPage";
}
The console line that is print is this:
.
.
.
[3/19/14 16:36:53:625 EDT] 0000006a SystemOut O
.
.
.
Does anyone know why this is not printing anything? Am I really not successfully sending anything to the controller?
After a good deal of reading and trial and error I found my answer. I will explain in terms with spring framework forms. In order to pass data through the form from front-end to back end, first every input will need to be tied to the form using the spring form JSTL tag
ex.
form => form:form
input=> form:input
in the form:form it isn;t necessary but you should have a modelAttribute that is linked to a java class, then in each input their need to be a path attribute the is linked to a variable in the modelAttribute class and a value to assign to the variable. Then on a submit the values are linked to the back end via the getters and setters on the java class to be used in the back-end. I hope I am explained that clearly.

Bind spring form to a list element

Is it possible to bind spring form:form to a list element? I've tried this way.
<form:form commandName="products[0]">
<form:input path="name"/>
</form:form>
also
<form:form commandName="products0">
<form:input path="name"/>
</form:form>
Where products list is populated in spring controller.
#RequestMapping(method = RequestMethod.GET)
public String getAll(Map<String, Object> map) {
map.put("products", productService.getAll());
return "products";
}
Received: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'products[0]' available as request attribute. Which as I understand means that spring haven't found where to bind the form.
No, this is not possible. The value you pass to the commandName attribute is a key and it is not resolved like a normal EL or SpEL expression would be. It is used directly. In other words, with
<form:form commandName="products[0]">
<form:input path="name"/>
</form:form>
Spring will look for a model attribute called products[0] which it won't find.
The alternative is to put the first element of the list in the model directly with a key you will use in your jsp.
Or you can use JSTL, get the first element in the list and create an HTML <form> element yourself.

JSP form:checkbox into a c:foreach

Similar problems are invoked in many posts in this forum; but no one has a solution that specific one, I thank you for helping me in this :
I'm using spring to develop a web application,
I don't know what I should put in the path of the form:checkbox tag which inside the c:foreach one, here is my code :
<c:forEach items="${persons}" var="person" varStatus="i">
<tr>
<td><c:out value="${person.firstName}" /></td>
<td><c:out value="${person.lastName}" /></td>
<td><form:checkbox path="person.rights" value="Download"/>Download </td>
<td><form:checkbox path="person.rights" value="Delete"/>Delete </td>
</tr>
</c:forEach>
'rights' is a list of Strings as it defined in the spring documentation, it has a getter and a setter like the other properties, my checkboxes work outside the c:foreach tag, but when including them into this tag this exception is generated :
org.springframework.beans.NotReadablePropertyException: Invalid property 'person' of bean class [java.util.ArrayList]: Bean property 'person' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
do you have an idea about what the problem is ??
This problem is strangely undocumented on most places. Here is an extract from the links I am posting below. The gist is that we need a static placeholder which maps to the type instead of the value of the bean. So anything inside a ${} will not work out. For this, and in the specific case of using a JSTL loop operator <c:forEach> with s[ring form tld, we should refer to the type information in each iteration using the varStatus attribute of the <c:forEach> operator, just like indices of an array, and thus refer to the inner properties of the iterable collection using . on the collection variable accessible via the outermost bean backing up the form.
For example:
<c:forEach items="${teamslist_session.teams}" var="team" varStatus="teamsLoop">
<form:input path="teams[${teamsLoop.index}].name"/>
</c:forEach>
where:
teamList_session is the bean backing up the form
teams is the collection of beans the properties of which we need to set in the path attribute
var is the a reference to each member of the teams collection
teamsLoop is the iteration index, which is used in the line below to refer to the say, ith element's bean's property called name
Please refer to the following links for more information:
Forum Discussion - See the last post
The link provided for reference in link 1

How to print a specific actionerror message with Struts2 validation?

When using Struts2 validation, when you put the <s:actionerror> tag in your JSP, the default behavior is to display all the action errors at that point in the page.
Is there a way to display only specific error messages at that point? For example, in the case of fielderror one only needs to add the fieldName attribute. Is there an attribute of actionerror that accomplishes similar behavior?
For field specific error the function is : hasFieldErrors()
You can use it like this:
<s:if test="hasFieldErrors()">
<div class="fieldErrors">
<!-- iterate through the fields errors, customize what you need -->
<s:iterator value="fieldErrors">
<s:property value="key"/>:
<s:iterator value="value">
<s:property/>
</s:iterator>
</s:iterator>
</div>
</s:if>
References:
Struts 2 documentation.
Interesting reading:
Why are my actionErrors and fieldErrors displayed with braces []?
https://www.mkyong.com/struts2/working-with-struts-2-theme-template/

Categories