Append a query string with action in Struts 2 - java

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>

Related

Pass values from html to Action Class Struts

I am completely new to Struts and now I am facing some issues.
Here is my html code :-
<div id = "processingMode">
<div id = "appButton">
<input type="hidden" value="Cancel" name="flagValue" />
Cancel
</div>
</div>
And I want to retrieve hidden field values in the action class (or please let me know how to pass values from html to action class).
Can anyone help me to achieve this.
In order to pass the hidden parameter value using struts
you first need to add the struts library and import the struts tag library
Try this below code
<s:url action="actionclass">
<s:param name="id" value="parametervalue" />
</s:url>
Hi Krupa
To send hidden parameters from view page to action class, you have to use <s:hidden></s:hidden> struts tag. You can pass the parameters through the URL too. But that is not a best practice to do so when it comes to security.
so try to use struts tag to achieve desired task. find the following sample app code to get a better understanding :-
View Page
<s:form action="testAction">
<s:hidden name="EmpName" value="DummyName" />
<s:submit value="Submit" />
</s:form>
Action class
public class Employee extends ActionSupport {
private String EmpName;
// getters and setters
public String execute() {
System.out.println("Employee Name :- "+this.getEmpName());
return "success";
}
}
struts.xml
<action name="testAction" class="Employee">
<result name="success"><path of your view page></result>
</action>
Simply,
Cancel
In Action class, create a parameter q and its set and get methods

Is it possible to set a struts token with a GUID as a token name?

I'm trying to set a struts token in a form with a unique name for each form. Is there way to set up the token in a way like <s:token name="<some random generated characters>". I was planning to use the TokenHelper.generateGUID() to generate the token name, if possible. I've tried setting a variable using <s:set var="tokenName" value="<%=TokenHelper.generateUID()%>"/>, then setting the token using <s:token name="${tokenName}"/>. I'm getting tld error about setting the in the tag. Here is the general code flow of the form.
here are the things that i've tried, but got the same result.
<%# page import="org.apache.struts2.util.TokenHelper" %>
<s:form action="actionName_method" name="actionName" method="post">
<s:token name="<%=TokenHelper.generateGUID()%>"/>
<s:hidden ....
.... rest of the fields go here ....
<s:submit value="save" name="submit"/>
</s:form>
Another one I tried is,
<%# page import="org.apache.struts2.util.TokenHelper" %>
<s:set var="tokenName" value="${f:generateGUID()}"/>
<!-- I defined generateTokenName as a tld function using the TokenHelper class -->
<s:form action="actionName_method" name="actionName" method="post">
<s:token name="${tokenName}"/>
<s:hidden ....
.... rest of the fields go here ....
<s:submit value="save" name="submit"/>
</s:form>
Here is the my definition of the function f:generateGUID() in the tld file.
<function>
<description>This will generate the a unique tokenName</description>
<name>generateGUID</name>
<function-class>org.apache.struts2.util.TokenHelper</function-class>
<function-signature>java.lang.String generateGUID()</function-signature>
</function>
Thank you in advance.
Yes, it's possible to set a token name with
<s:token name="%{tokenName}"/>
It will generate two hidden fields one for the token name and another for the token value. Make sure the value of the first field corresponds to the name of the second field.
The action property tokenName is initialized like
tokenName = TokenHelper.generateGUID();
or
tokenName = UUID.randomUUID().toString();
Also make sure the form is using POST method.

Change action attribute of Form for different action methods in Struts2

I have created a from in JSP page name add.jsp to save data like this
<s:form action="AddDomain">
<s:push value="idp">
<s:textfield name="domainName" label="Domain Name" />
<s:textfield name="url" label="Domain URL" />
<s:textfield name="noOfLicense" label="License Purchased" />
<s:textfield name="licenseExpireDate" label="License Expire Date" title="YYYY-MM-DD like 2013-01-21" />
<s:textfield name="userActiveDuration" label="Active User Duration"
title="please mention in days" />
<s:textarea cols="30" rows="5" name="notes" label="Note"></s:textarea>
<s:submit value="Add"></s:submit>
</s:push>
</s:form>
Action Method that show this view is as
public String addDomainPage() {
return ActionSupport.SUCCESS;
}
I have created another page that list the all domains and provide a edit link to edit any domain. When Use click on edit URL this action is called
public String loadDomain() {
HttpServletRequest request = ServletActionContext.getRequest();
String url = request.getParameter("durl");
IDPBroker broker = new IDPBroker();
idp = broker.getDomainByURL(url);
return ActionSupport.SUCCESS;
}
On successful completion of action I show the add.jsp page. Struts populate the data in JSP page.
Now, issue is that I want to change the value of action attribute of form tag. I also want to change the value of submit button to 'Edit'. I have plan to create some private attribute(action,Label) in Action class and when an addDomainPage action is call I will change the value of these attribute with respect to add page. Similar for loadDomain action. Now I don't know how to do this means how to use these private attributes in view. Tell me is I am doing correctly and what to do next?
The same action class could be used to map different methods on submit buttons. Like
<s:submit value="Add" method="addDomainPage" />
<s:submit value="Load" method="loadDomain" />
The form action attribute should map to the action class execute method which will never call if you use submit buttons like that. The DMI which is enabled by default allows to call specified methods.
If you want to dynamically change attributes in the Struts tags you could use OGNL expressions in JSP instead of hardcoded values. For this purpose you should define properties in the action that define dynamic values before result is executed. For example
public String getAction(){
return "AddDomain";
}
<s:form action="%{action}">

Unable to use hidden field value in JSP

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}"/>

Struts2 anchor tag doesn't include contextPath

%{#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....

Categories