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/
Related
How can I retrieve a dynamically generated name tag for <s:set> on Struts 2?
E.g.:
In my action class I have an object (currency) that have this fields: 'id' and 'symbol'.
So in my view page I can use this:
<s:iterator value="currency">
<s:set name="var_%{id}" value="symbol"/>
<s:set name="total_%{id}" value="%{0.0}"/>
</s:iterator>
So I can use this to create these PageContext variables like "var_BRL", "var_USD", "var_MXN" and "total_BRL", "total_USD", "total_MXN" etc. If I write the following code:
<s:property value="#var_USD"/> = <s:property value="#total_USD"/>
I'm able to retrive a result like:
USD = 0.0
I'm using these #total_XXX variables to sum some values under some conditions presented by another iterator, in a way that I'll have at the end of this other iteraction, a result of the total spent in every currency (BRL, USD, MXN etc).
But when I try to retrieve these values dynamically, nothing is rendered. Following is the code I'm using to retrieve the values from these variables, at the end of my page. I don't understand OGNL very well, so I've tryed different arrangements like the ones below and had no success with any of them:
<s:iterator value="currency">
<s:property value="#var_%{id}"/> = <s:property value="#total_%{id}"/>
<s:property value="#%{var_%{id}}"/> = <s:property value="#%{total_%{id}}"/>
<s:property value="%{#var_%{id}}"/> = <s:property value="%{#total_%{id}}"/>
</s:iterator>
Is there a solution to retrieve these values in the PageContext? Or can I only solve this inside my Action? I've searched so many posts but I couldn't find anything.
Thank you!
LZ
discussing with a friend, I found the proper OGNL expression to call for the dynamically created variables names at the end of my code:
<s:iterator value="currency">
<s:property value="#attr['var_'+id]"/> = <s:property value="#attr['total_'+id]"/><br>
</s:iterator>
(Oups.. Sorry for my english :) )
In my web application, Struts2 is used as the main Servlet dispatcher and filter. But For some reasons, i have a custom filter and a custom servlet used for a specific url "/book".
But I have some commons jsp... i had some issues when the custom Servlet should display my request attributes in the JSP because of the struts tags (implemented before). So i changed these tag by the jstl taglibs and it works now.
But... In one JSP, the main (lol)... I have a search form.. This JSP is included in several JSPs and could be called by Struts and the custom Servlet..
With only Struts the tag was "< s:form>.." and when the form was submitted, all sended values was kept in the input... But now, because of the custom Servlet i use a simple html form which is calling the struts action "search.do".
As source code is below:
<form method="post" action="<c:out value="${contextPath}"/>/search.do" name="search" id="search">
<input type="text" id="search_searchWord" value="" maxlength="200" size="100" name="searchWord">
<div align="right">
<input type="submit" value="Ok" name="searchButton" id="search">
</div>
<select id="search_searchCrit" name="searchCrit">
<option value="0">Crit1</option>
<option value="1">Crit2</option>
<option value="2">Crit3</option>
</select>
</form>
My problem is the search word and the selected option are refreshed after the submit. I need to keep them !
Is there a way to use the struts taglibs with a Standard Servlet ?
Or Do you have another solution to keep the submitted information ?
Thanks all !
take each field value from the input field and write js function to fill each field in jsp source code of your page.
function selectedValue(){
var value =<%=request.getParameter("searchCrit")%>;
if(value !=null)
{
document.getElementById('search_searchCrit').innerHTML=value;
}
}
I found a solution with the help of #java_seeker.
In my Struts action, i got the request through this way :
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("searchWord", this.getSearchWord());
There is two different way to do this, see: http://www.mkyong.com/struts2/how-to-get-the-httpservletrequest-in-struts-2/
The attribute is setted in each method (in the action) that could refresh the page.
Then, i just recovered and set the attribute from the request as a variable with a jstl tag and display it as the value of my html input:
<c:set var="searchWord" value='<%=request.getAttribute("searchWord") %>' />
<input type="text" id="search_searchWor" value='<c:out value="${searchWord}" />' name="searchWord">
For the , i just used an <c:choose><c:when test=""></c:when><c:otherwise><c:otherwise></c:choose> to set the selected choice.
Now all value are always displayed. Maybe it's not the very good way to display share the same JSP between a standard servlet and a Struts action, but it works. I'm open to try a better solution if you have one! Thanks all!
I am appending the parameters with action but I am getting an exception of on my Struts 2 page.
PWC6212: equal symbol expected
Below is my action with appended parameters code which is to be submitted.
action="MyAction.action?id=<%=request.getParameter("id")%>&name=<%=request.getParameter("name")%>&age=<%=request.getParameter("age")%>&num=<%=request.getParameter("num")%>"
Is the above is the syntax problem? If not then how can we set the parameters as a query string with action?
You should not use Scriptlets (<%= %>)
And, if action is an attribute of a Struts tag (like <s:form>), you can't use scriptlets, you should use OGNL.
Please refer to this question: Struts 2 s:select tag dynamic id for more details
Assumed the action attribute is used with the <form tag. Then the construction
<form name="MyAction.action" action="upload?id=<%=request.getParameter("id")%>&name=<%=request.getParameter("name")%>&age=<%=request.getParameter("age")%>&num=<%=request.getParameter("num")%>" method="POST">
should work with the current context. But in your case given error message (Exception Name: org.apache.jasper.JasperException: equal symbol expected is occurred when <s:form tag is used. So, you cannot use this url in the action attribute. This attribute should contain the plain action name that would be used to find your action.
"How we set parameters as a querystring?"
Actually we do it with <s:param tag. For example, when using hyperlinks
<s:a action="MyAction">
<s:param name="id" value="%{id}"/>
<s:param name="name" value="%{name}"/>
</s:a>
But this construction doesn't work with <s:form tag, unless you applying a special syntax as described in this answer and you definitely want to get these parameters if you do in the action
String quesyString = request.getQueryString();
and this string should not be empty.
However, this usecase is rarely applied, If you don't have a reason to get parameters in such way then as alternative you always can use <s:hidden fields to contain the values of the parameters. For example
<s:form action="MyAction" method="POST">
<s:hidden name="id" value="%{id}"/>
<s:hidden name="name" value="%{name}"/>
</s:form>
These values are passed as a parameters and initialize the action attributes after params interceptor worked. You could also get this parameters directly from the request as
Map<String, String[]> params = (Map<String, String[]>)request.getParameterMap();
However, the more convenient way to do this in your action is to implement ParameterAware.
Just like #AndreaLigios mentioned, you should use Struts2 specified EL, check out the Document here.
If you are using <s:url/>, check out Document please for more information.
Your code should look something like this:
<s:url value="MyAction.action">
<s:param name="id" value="%{#parameters.id}" />
<s:param name="name" value="%{#parameters.name}" />
<s:param name="age" value="%{#parameters.age}" />
<s:param name="num" value="%{#parameters.num}" />
</s:url>
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.
%{#request.contextPath} doesn't work inside an s:a tag in Struts2. (Struts 2.2.1 to be specific.) Is there a way to make it work? It works in other Struts2 tags.
Here are two lines in a JSP file in a Struts 2 project whose context path is "/websites":
<s:a href="%{#request.contextPath}/clickme" theme="simple">Click here.</s:a>
<s:form method="post" action="%{#request.contextPath}/submitme" theme="simple"></s:form>
And here is the output:
Click here.
<form id="submitme" name="submitme" action="/websites/submitme" method="post"></form>
Notice that the context path is left off the anchor but is included in the form.
P.S. I can't use ${#pageContext.request.contextPath} here because ${} isn't allowed in Struts2 tags. Besides, I'm trying to be consistent. And I also try generally to avoid ${} since it does not auto-escape the output.
Thanks!
This should work:
<s:set id="contextPath" value="#request.get('javax.servlet.forward.context_path')" />
<s:a href="%{contextPath}/clickme" theme="simple">Click here.</s:a>
However, you're not supposed to do this. When you need an url, use the <s:url> tag:
<%-- Without specifying an action --%>
<s:url id="myUrl" value="clickme" />
<s:a href="%{myUrl}" theme="simple">Click here.</s:a>
<%-- With an action --%>
<s:url id="myUrl" action="clickme" />
<s:a href="%{myUrl}" theme="simple">Click here.</s:a>
By the way, you don't need a context path for the action attribute of a form:
<s:form method="post" action="submitme" theme="simple"></s:form>
Does Struts 2 support EL?
You can use ${request.contextPath} if it does....