how to trigger an action by clicking a link in Struts2 - java

I am coming rails framework experience. I have a simple problem.
I have a link on a page. I want some Struts2 action executed when the user clicks the link and want to pass along some parameters.
For example:
link on page = My Link
Action I want to call = myTestAction (it is defined in struts.xml)
parameter I want to pass = typeA=false
How can I do this? I looked at <s:url><s:param name="typeA" value="false"></s:url> tag. However, the parameter doesnt seem to get pass. when I hover on the link i do not see any parameters.

Try This..
<s:url id="url" action="myTestAction">
<s:param name="typeA">false</s:param>
</s:url>
<s:a href="%{url}" >My Link</s:a>

the action class needs to have setTypeA(String value) and getTypeA() declared in it for the above code to pass typeA=false
I dont know why they would do this...or maybe rails has spoiled me.

<s:url action="actionNameInStrutsXML" method="methodNameInYourClass" var="menuAdmin" />
<s:a href="%{menuAdmin}">Menu</s:a>
In this way you can call the method that you prefer.
Remember to put in your struts config this:
struts.enable.DynamicMethodInvocation" value="true"

I think #vinayak answer is perfect if you want to send more than one parameter use escapeAmp=false

Related

Struts 2 Page Include Access Request Object Attribute From Property Tag

I am including a JSP page using the s:include tag:
<s:include value="/WEB-INF/jsp/centers/tpa/admin/users/UserEmployerAccessRow.jsp" />
I have several objects that I want to make available to this include and I am trying to store them into the request before the include happens. I am using the s:set tag to store to the request object:
<s:set var="employer_tmp" value="employer" scope="request" />
Everything in the jsp works as expected up to this point. The included jsp is not able to access objects in the request from a s:property tag. Here is what I have inside of the UserEmployerAccessRow.jsp:
<s:property value="#request[employer_tmp]" />
I have also tried it this way:
<s:property value="#employer_tmp" />
I have verified that the object is in the request by doing this:
<% out.println(request.getAttribute("employer_tmp")); %>
My guess is that the s:property is looking for the internal map that Struts sets up for the request and not looking at the actual request object. Does anyone know any markup to force the s:property to grab something out of the request object? It seems like overkill to have to run another action in the loop that I have this include in. I cannot use s:param to hand parameters to the include because it only handles simple http parameters and not objects. Thanks in advance for any direction you guys can provide!
I ended up finding the problem. The attribute name needs to be quoted in the s:property tag:
<s:property value="#request['employer_tmp']" />

How to pass Struts2 property tag as parameter in java function in the jsp?

I am having a problem in passing Struts2 property tag as parameter in java function in the jsp.
I am calling a java function like this from jsp,
<s:if test="%{DoSomething()}">
DoSomething() function is in the Action class, and is being called for each record in the iterator.
The problem is that I want to pass this property <s:property value="userId"/> in the function DoSomething as java string as follow.
<s:if test="%{DoSomething("<s:property value="userId">/)}">
Can anyone please guide me that how can I do that. I goggled it but did not find anything.
<s:if test="%{DoSomething(userId)}">
Most likely you wouldn't need to pass in a parameter like this. You are trying to call a method DoSomething from your action class. You want to pass in a parameter to this method in your action class. The userId must be existing already in you action class otherwise you couldn't be able to get it through the s:property tag. You can just use this userId variable in your DoSomething method of your action class.
you cannot used Struts2 tag to another Struts2 tag, you can use expression language,
first use <s:set name="userId" value="userId" /> then use ${userId}

How to get JSP custom value inside another JSP custom tag

I got two tags as below:
Tag 1 this will return a string that can be displayed on browser:
<dn:account><%= account_username %></dn:account>
Tag 2
<token:output username=""></token:output>
Instead of displaying the tag 1, I want pass it to tag 2 like below:
<token:output username="<dn:account><%= account_username %></dn:account>"></token:output>
The problem is I'm not getting the value of the username, I'm getting this entire tag return back.
What are the solutions.
TQ.
To get values there is &{}
<c:set var="aVariable" value="aValue" />
<tt:anotherTag attr1="${aVariable}" />
check this out if there ia any problem let me know.
By the way check this. http://docs.oracle.com/cd/E19159-01/819-3669/bnalj/index.html
With the previous annotation I think it´s not good enough, you need to specify the scope as request.
<c:set var="aVariable" value="aValue" scope="request" />
I found an answer.
Its should be like this:
<dn:account>
<token:output username="<%= account_username %>"></token:output>
</dn:account>
Thanks for those helping.

Websphere Commerce-TypedProperty

Can anyone help me to understand the usage of TypedProperty in websphere commerce?
ie,How to pass values from one jsp to other using TypedProperty without a command class.I would prefer to handle it in my client side itself without invoking Command class..can anyone help me to sort out it?
Typed property is usually used to pass values from controller commands to JSPs. If you just want to pass values from one JSP to another, create a form in your first JSP and submit it to the second.
If this is a form submit, set the values you need to pass in element. In the results jsp you can get those values using ${WCParam.xxx} .
FYI - To list out all the values in WCParam object try to print the below in JSP :
${WCParamValues}
We use typedProperty when we need to send anything from the command. For example, you give an order ID from the first JSP and want to get the final amount to be passed the result JSP. Here in the command we use the orderID from the request object -> Then we use the OrderAccessBean to get the OrderTotal -> then we set this to a TypedProperty object -> we then set this TypedProperty object to request properties using setRequestProperties() OOB method in a controller command.
Hope this makes it clear !
TypedProperty is a class in Java which can be compared to Hashmap in Java for better understanding. It is a name value pair combination.
I just wanted to understand the problem before answering further.
Why do you want to use TypedProperty in Jsp to pass the value from one jsp to another?
Are you importing the second jsp or including the second jsp to which you have to pass the values to?
If you are importing, you can use c:param tag to pass the values to the second jsp.
For included jsps, the values are already available in the second JSP.
Please include code snippets to explain your problem so that it can be answered clearly.
You can pass parameters from one jsp to another by using the following code snippet:
<c:import url="child.jsp">
<c:param name="name1" value="value1" />
<c:param name="name2" value="value2" />
<c:param name="name3" value="value3" />
</c:import>
Within the child.jsp you can read the parameters by using:
<c:out value="${param.name1}" />
<c:out value="${param.name2}" />
<c:out value="${param.name3}" />
A TypedProperty is nothing but a Wrapper to HashMap. So that's nothing to do here with passing values from one JSP to another JSP. Without invoking a command, you can't pass a Java object to another JSP.
And that is the very basic of Command Framework. I would prefer to go with the first answer.

Can I call a Struts2 action on click of a html link in html page

I want a Struts2 action to be performed on click of a html link in a html page.
My first question is whether is it possible to perform a Struts2 action in a html page(not JSP)?
If yes, take a look at my code below:
home.html
href="home.action"
struts.xml
action name="home" class="com.struts.action.HomeAction"
result name="Success">loginJSP.jsp
*****web.xml*****
I did filter mapping such that everything goes to Struts2
Have you tried this?
click here
Or this?
Click here<br />
Also see: http://struts.apache.org/2.x/docs/url.html
Just curious to know, why can't you use JSP?
<s:url action="actionNameInStrutsXML" method="methodNameInYourClass" var="menuAdmin" />
<s:a href="%{menuAdmin}">Menu</s:a>
In this way you can call the method that you prefer.
Remember to put in your struts config this:
struts.enable.DynamicMethodInvocation" value="true"

Categories