I have a hidden field whose value I want to send to my action, but it always sends null.
<s:hidden property="SystemId" name="SystemId" id="SystemId" value="1"/>
I am calling the action like this
<s:url id="papa" value="Alertpup?ET_ID=%{ID}&ET_BUSID=%{SystemId}"></s:url>
<s:a href="%{papa}"><s:property value="ID" /></s:a>
But my ET_BUSID field in from bean will be null.
Instead of hidden try with
<s:set name="SystemId" value="1"/>
<s:url id="papa" value="Alertpup?ET_ID=%{ID}&ET_BUSID=%{#SystemId}"/>
Related
I'm trying to assign the value of radio button ans1 to another variable ans_submit.
<s:form action="ques2" namespace="/questions" theme="simple">
Question 1: <s:property value="#session.ques1.question"/><br/><br/>
<s:radio list="#session.ques1.option1" name="ans1" value="%{'option1'}" label="1. "/><br/>
<s:radio list="#session.ques1.option2" name="ans1" value="%{'option2'}" label="2. "/><br/>
<s:radio list="#session.ques1.option3" name="ans1" value="%{'option3'}" label="3. "/><br/>
<s:radio list="#session.ques1.option4" name="ans1" value="%{'option4'}" label="4. "/><br/>
<s:submit name="submit" value=" Next"/>
</s:form>
<s:set var="ans_submit" value="%{#ans1}" scope="session" />
But when I retrieve ans_submit in action class, it is giving null.
I have to keep <s:set> tag outside of <s:form> tag. Because there is no variable defined as such in my model.
I don't want to use javascript!
Use a map to populate a radio tag.
<s:radio name="ans1" list="#{'option1': '1. ','option2': '2. ','option3': '3. ','option4': '4. '}" />
In the action class you should have a property for ans1 where the value would be set when you submit the form. This property is local to the action instance which is created every time when you make a new request. So, you need to save the value in the session.
session.put("ans1", ans1);
Now to retrieve this value from session in another action you can initialize the ans1 property
ans1 = (String) session.get("ans1");
or you can preset the value in the radio tag using a value attribute.
<s:radio name="ans1" list="#{'option1': '1. ','option2': '2. ','option3': '3. ','option4': '4. '}" value="%{#session.ans1}"/>
I have a hidden value in JSP.
<input type="hidden" name="selectedtxn" value="" />
And I access the value from servlet.
String selectedTxnNo = request.getParameter("selectedtxn");
But the value I get in servlet is undefined.
Am I doing anything wrong?
your hidden input field is empty value="" thats why you are getting undefined.Give some value like value="test"
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>
How to get a value that is sent by a form in jsp.
This is because you are not sending Name back to the controller.for sending values back to the action all you need to send them as form fields where as in your case you are just displaying them but not sending them back.
One solution is to store the name as a hidden field like
<s:form action="AddProduct" >
<tr><td>
<label for="name">Name:${Product.name}</label>
<s:hidden name="name" value="%{Product.name}"/>
</td></tr>
<s:submit/>
</s:form>
Make sure you should have name property in your action class or if you have a bean in your action class who has name property than the name of hidden field shd be beanName.name
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()}.