3

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
JN_newbie
  • 5,492
  • 14
  • 59
  • 97

3 Answers3

3

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

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
2

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.

Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

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>
Jaiwo99
  • 9,687
  • 3
  • 36
  • 53