How can i send a hidden value to action class using Struts2 - java

I need to send a value to action class when a click on a link. This is my code:
<s:iterator value="results" var = "item">
<li><pre>
<b></b><s:property value="%{#item.getGroupName()}"/> (<span class="count"><s:property value="%{#item.getNrCompanies()}"/></span>)<s:hidden value="%#item.getGroupId()}" name="GroupId" id="GroupId" />
</pre></li>
</s:iterator>
I need to be sent back %#item.getGroupId()} depending on the option I choose from iterator.
<s:hidden value="%#item.getGroupId()}" name="GroupId" id="GroupId" />
I have the getter and setter for Group Id but it's does't reach action class.
Thank you

Is it ok if the group id appears in the user's browser's address bar? if so, then you can add params to a url like this:
<s:iterator value="results" var = "item">
<s:url var="companyUrl" action="getCompanies">
<s:param name="groupId" value="%{#item.getGroupId()}"/>
</s:url>
<li>
<pre>
<a href="${ companyUrl }" class="selectable">
<b><s:property value="%{#item.getGroupName()}"/></b>
(<span class="count"><s:property value="%{#item.getNrCompanies()}"/></span>)
</a>
</pre>
</li>
</s:iterator>

<s:hidden value="%#item.getGroupId()}" name="GroupId" id="GroupId" />
try the below one it seems you had not open { braces
<s:hidden name="groupId" value="%{#item.getGroupId}"></s:hidden>

i think the problem you have may be due to name="GroupId". Change it to name="groupId".
Also change value="%#item.getGroupId()} to value="%{#item.getGroupId()}.

Related

How to use spring:message in a forEach loop

I have been working on a system that provides bilingual support to a website from a database using the <spring:message /> tag library.
I can read/write to the applications en/fr properties files. When I hardcode what would be a new key <spring:message /> will display it correctly. Ex) <spring:message code="f12345' /> will display "test data".
What I am having problems doing is using a dynamic key. No matter how I try to attach the key it fails.
Here are the methods I have tried group by end result.
500 error org.apache.jasper.JasperException: (line: [104], column: [29]) [quote/equal] symbol expected
<spring:message var><c:out value="${CLUObject.SpringKey_name()}" /></spring:message>
<spring:message var='<c:out value=\"${CLUObject.SpringKey_name()}\" />" text="wrong" />
<spring:message code="<c:out value="${CLUObject.SpringKey_name()}" />" text="${CLUObject.SpringKey_name()}" />
<spring:message code=<c:out value="${CLUObject.SpringKey_name()}" /> text="${CLUObject.SpringKey_name()}" />
<spring:message code= <c:out value="${CLUObject.SpringKey_name()}" /> />
Next we have the non-server crash which simply causes the table to not display
<spring:message var='<c:out value="application.message" />' arguments="${CLUObject.SpringKey_name()}" />
<spring:message var='<c:out value="${CLUObject.SpringKey_name()}" />' text="wrong" />
<spring:message code="<c:out value=\"${CLUObject.SpringKey_name()}\" />" />
<spring:message code="${CLUObject.SpringKey_name()}" />
<spring:message code="messageCode" arguments="$value1}" />
<c:set var="temp" > <c:out value="${CLUObject.SpringKey_name()}" /> </c:set><td><spring:message code="messageCode" arguments="${temp}" htmlEscape="false" /></td>
The best I have managed to get to simply displays the text of the key instead of the value. Which can already be done using <spring:message text="${CLUObject.SpringKey_name()}" />'
In the applicationResources file
messageCode=Test message for {0}.
Then inside the jsp page
<c:set var="temp" > <c:out value="${CLUObject.SpringKey_name()}" /> </c:set>
displays "Test message for CLUVALUE.C1111."
I found one site online which appeared to be doing the same thing.
<form:select path="${path}">
<c:forEach var="i" items="${items}">
<form:option value="${i[itemValue]}">
<c:choose>
<c:when test="${localize}">
<spring:message code="${i[itemLabel]}" text="${i[itemLabel]}"/>
</c:when>
<c:otherwise>
<c:out value="${i[itemLabel]}"/>
</c:otherwise>
</c:choose>
</form:option>
I have found a second example where they have a dynamic key with the spring message being used in a forEach loop. Search for spring:message and its 14/17.
<c:forEach items="${errors.allErrors}" var="error">
<spring:message code="${error.code}" text="${error.code}"/><br/>
</c:forEach>
I have found a third example.
In the three examples I have found, the spring:message are all used the same, with the code and text attributes being the same.
I cant see the output, and I cant be sure that their text argument isnt being displayed instead.
Here is the controller block
List<CLU_STRUT> myCLUs = cluService.BuildCLUs();
model.addAttribute("CommonLookUp", myCLUs);
And here is the JSP
<c:forEach var="CLUObject" items="${CommonLookUp}" varStatus="vs">
<tr>
<c:set var="temp" > <c:out value="${CLUObject.SpringKey_name()}" /> </c:set>
<td><spring:message code="messageCode" arguments="${temp}" htmlEscape="false" /></td>
<td><c:if test="${CLUObject.getCountNew() gt 0}"> <a href='drillview?drillvalue=${CLUObject.SpringKey_name()}&mode=drill&drilltype=I'><c:out value="${CLUObject.getCountNew()}" /></a></c:if><c:if test="${CLUObject.getCountNew() eq 0}">0</c:if></td>
<td><c:if test="${CLUObject.getCountMod() gt 0}"> <a href='drillview?drillvalue=${CLUObject.SpringKey_name()}&mode=drill&drilltype=U'><c:out value="${CLUObject.getCountMod()}" /></a></c:if><c:if test="${CLUObject.getCountMod() eq 0}">0</c:if></td>
<td><c:if test="${CLUObject.getCountDelete() gt 0}"> <a href='drillview?drillvalue=${CLUObject.SpringKey_name()}&mode=drill&drilltype=D'><c:out value="${CLUObject.getCountDelete()}" /></a></c:if><c:if test="${CLUObject.getCountDelete() eq 0}">0</c:if></td>
<td><c:out value="${CLUObject.getCountTotal()}" /></td>
</tr>
</c:forEach>
TLDR; How to use spring:message where the key is dynamic in a jsp page.
I finally figured out what the issue was.
Short Answer: the inversion of control was causing things to happen now how I expected. I was trying to access a function or member, and it kept trying to use a get version of it. Since I did not have specifically getmember() to access the variable, it was doing strange and wrong things.
By introducing public String getspring_name() {return spring_name;} all of a sudden the <spring:message code="${CLUObject.spring_name}" /> works.
Long Answer: In another section of my code I discovered what I thought was a private string member being accessed in a jsp page. After considerable testing of other private variable types and functions, my original thought that I was gaining access to the private member was disproved. Yet, this specific one was being accessed. After more testing I discovered when I had a get function in a very specific naming convention this behavior was reproducible. So in this case I had a private String CLU_Name, and was lucky enough to have created the get function as getCLU_Name(). Having named other get functions like getCountDelete() for Count_Delete was not allowing me to reproduce the behavior for THEM, because they did not fit the naming pattern.

Append a query string with action in Struts 2

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>

Struts2: values from action are not seen in jsp, but they are seen in the javascript code from jsp

In action I have a variable which has getter on it.
private String myActionVariable = "predefined....";
public String getMyActionVariable () {
return myActionVariable;
}
In jsp, I try to use my variable in this way:
<input type="button" class="styledButton"
onclick="javascript: doAjax('myActionName',false);"
value="${myActionVariable}"
But it is not shown. However, if I output this variable from the javascript code included within the same jsp file:
alert (${myActionVariable})
I will get the value of it....
Any idea please ? ...
You can use a Standard <input/> HTML Tag with an <s:property /> Struts2 Tag for the value, like this:
<input type="button" class="styledButton"
onclick="javascript:doAjax('myActionName',false);"
value="<s:property value="%{myActionVariable}"/>"/>
or a Struts2 Tag directly like this:
<s:submit type="button" cssClass="styledButton"
onclick="javascript: doAjax('myActionName',false);"
value="%{myActionVariable}" />
Note that with Struts2 Tag, class attribute becomes cssClass (and style becomes cssStyle), and that %{} is the right OGNL syntax, instead of ${} (that is JSTL syntax)
EDIT: when using Struts2, forget about JSTL, you won't need them anymore.
You should be using struts2 tag.
<input type="button" class="styledButton" onclick="javascript: doAjax('myActionName',false);" value="${myActionVariable}">
Instead of this, use
<s:submit type="button" cssClass="styledButton" onClick="javascript: doAjax('myActionName',false);" value= "myActionVariable" />

Conditional if in struts2 tags

I have the following code.
<s:push value="#session['person']">
<s:if test="%{admin=='y'}">
<a class="add" href="/projit1/project/addProject.jsp">Create a Project</a>
</s:if>
</s:push>
I am trying push an object person from session map to valuestack and check one of its properties admin's value. If it is 'y' then the link "create a project" appears.
But this code is not working. If i use the above code, both admins and normal members does not see the link. what could be the problem ? please help
Try this.
<s:push value="person">
<s:if test="%{admin=='y'}">
<a class="add" href="/projit1/project/addProject.jsp">Create a Project</a>
</s:if>
</s:push>
And you have a getAdmin() or isAdmin() on your Person object I assume? If that's the case, I also assume the method returns a char 'y'? I suggest you try displaying the result of calling admin and go from there. e.g.
<s:push value="#session['person']">
admin: <s:property value="%{admin}" />
<s:if test="%{admin=='y'}">
<a class="add" href="/projit1/project/addProject.jsp">Create a Project</a>
</s:if>
</s:push>

Use object's method in a jstl page

I have a model with this method:
public List<String> getPriviledgeNames()
I'm trying to display a list of checkbox in my jstl page, and I'd like to checked only the ones which their name is in the list returned from my model.
So I have something like:
<li>
<input type="checkbox" name ="priviledge" id="CheckBox2" class="checkbox" value="clickthrough" />
<label for="CheckBox2">Clickthrough</label>
</li>
<li>
<input type="checkbox" name ="priviledge" id="CheckBox3" class="checkbox" value="details" />
<label for="CheckBox3">Details</label>
</li>
I'd like to add the checked="checked" only their name is in the list provided by my model's method. Is there a way to do this WITHOUT using scriptlet? I'd like to have no java code inside my pages...
Thanks for any helps!
Roberto
<c:when test = "${fn:contains(list,value)}">
-according to the docs it's actually doing a string/string comparison, so I assume it's actually checking whether the string value is a substring of list.toString().
You could also do a manual test on each value in the list with a forEach tag but that would be messier.
By using a map instead of a list could do something like this:
<c:if test="${yourModel.priviledgeNames['clickthrough'] ne null}">
checked="checked"</c:if>
Or just this: ${yourModel.priviledgeNames['clickthrough']} by mapping your checkbox name to checked="checked"

Categories