Im a Java newbie, I have this code working which obtains the brandCode parameter and places in a hidden input named "brandCode"
<html:form method="get" action="/catalogindexsearch.do" styleId="sortAndNavigationForm">
<input type="hidden" name="formAction" value="searchDisplay" />
<html:hidden property="brandCode" />
</html:form>
But I wish to use the parameter elsewhere in my JSP i.e.
<h1>Brand Code: ${brandCode}</h1>
So my question is how can I create a Java variable from the html:form URL parameter?
In JSTL you can access a form property by form name
<h1>Brand Code: ${formName.brandCode}</h1>
Give it an ID like this:
<html:hidden property="brandCode" id="brandcode" />
then access the value by:
var brandCode = $("#brandcode").val();
or if you're not using jQuery:
var brandCode = document.getElementById("brandcode").value;
If you're trying to use the value of a property from your action class, you had it right - It's
${brandcode}
where brandcode is a property in your action class with getters and setters:
private String brandcode;
public String getBrandCode() {
}
you get the point.
What I was after in the end was this ${param.brandCode} or this ${param["brandCode"]}.
Related
My requirement: In Struts 2, dynamically generate form element name by using member value.
I had tried with: <s:textarea name="employee_<s:property value='employeeNumber'/>"/>
Resulting code in web browser: <textarea name="employee_<s:property value='employeeNumber'/>"></textarea>
My expectation in web browser code: <textarea name="employee_101"></textarea>. 101 as an employeeNumber is only used for demonstration only.
Please help, thanks!
In Struts2 you can always use OGNL expression in the Struts tag's attribute. Just provide the value from the value stack. You should know that the action bean is on top of it, and having a getter for employeeNumber should return desired value 101.
<s:textarea name="employee_%{employeeNumber}"/>
Also be aware of Struts doesn't allow nested tags in the attributes in favor of OGNL.
try this,it working fine.
MyAction.class
private int property_value;
public int getProperty_value() {
return 6;
}
public void setProperty_value(int property_value) {
this.property_value = property_value;
}
result.jsp
<s:set var="xyz" value="property_value" />
<s:textarea name="emap_%{#xyz}" />
I have a problem with struts property, pl check my sample code.
in struts action class i have a property that is name2,
SampleAction.java
public class SampleAction extends ActionSupport
{
private String name2;// setter and getter is also there
}
if i use like below i ll get the value
<s:property value="name2"/>
I wan to get the name2 value in jsp in dynamic way,
but here i need pass the value 2 in dynamic way some thing like below..
<s:set name="nameNumber" value="2" />
<s:property value="name%{#nameNumber}"/>
How can i achieve that.
Thanks in Advance,
Prabhakar Manthena
I am using ApplicationAware in my struts 2.0 action class. So, i placed an entry in then application map as
application.put("animalDTO", animalDTO);
animalDTO:
public class AnimalDTO {
private String name_N1;
private String name_N2;
private String name_N3;
private String name_N4;
// getters and setters
}
Now before going to my jsp page i am populating the values in each of the variables.
In JSP page: i am using iterator to get N1, N2 till N4.
Now i need to create s:textfield and populate these variables content DYNAMICALLY.
ie.
<s:iterator value="application.nameList" var="nameList">
<s:textfield theme="simple" value="%{application.animalDTO.name_#nameList}" />
</s:iterator>
Here, i am not getting any values populated.
If i hard code as -> value="%{application.animalDTO.**name_N1**}"
Then all the text-fields will be populated with value of name_N1, but that not what i need. I need for fetch the values dynamically.
Can anyone help me with this OGNL requirement?
Thanks in advance
=====================
I tried this also, i created string variable which is of the required ognl format, but i dont know how to make the string as a lookup to valuestack. This is what i did:
<s:iterator value="application.nameList" var="nameList">
<s:set var="varUrl" value="%{'application.animalDTO.name_' + #nameList}" />
<s:property value="varUrl" />
<s:textfield name="NAME__%{#nameList}" theme="simple" id="NAME-%{#nameList}" value="%{varUrl}" />
</s:iterator>
but instead the text box is getting values like:
application.animalDTO.name_1
application.animalDTO.name_2
application.animalDTO.name_3
application.animalDTO.name_4
what is need is the values corresponding to these variables.
Any idea how to convert this string as an object/key to lookup into valuestack.
Finally i got the solution. I tried many trail and error methods. And finally hit the answers. We can dynamically read the property values by doing:
<s:iterator value="application.nameList" var="nameList">
<s:textfield theme="simple" value="%{application.animalDTO['name_' + #nameList]}" />
</s:iterator>
I tried %{application.animalDTO['name_' + #nameList]} and it worked. :-)
I have a struts2 action with a field: private Long omradeId;
The field has a getter.
The action is sent to a jsp and within that jsp i can access the field using <s:property>tag. Thats all good.
Now i also have within the jsp a section where i define a <script>. Within that script i would like to create a variable that will build a url with the above mentioned struts2 field as a value.
<script type="text/javascript">
var url = "/path/to/action?parameter1=";
</script>
How can i put the value of omradeId after the equals (=) sign? I tried using <s:property>but that did not work.
Any suggestions?
"/path/to" will change depending on the web server. To avoid this use the struts2 url tag.
See: http://struts.apache.org/2.x/docs/url.html
For an action called "action" in namespace "/" with a parameter called parameter1 having the value omradeId, you would simply say:
<s:url namespace="/" action="action">
<param name="parameter1" value="%{omradeId}"/>
</s:url>
putting the above into the JS variable we have:
var url = "<s:url action="action"><param name="parameter1" value="%{omradeId}"/></s:url>";
Using the above will mean your application can be installed on different application servers without change.
Having formated xml is nicer than inline, if using a lot of parameters adding the var parameter to the s:url tag to give it a name and then you can reference this string in a number of places with the s:property tag would keep things clean.
<s:url namespace="/" action="action" var="myString">
<param name="parameter1" value="%{omradeId}"/>
</s:url>
var url = "<s:property value="#myString"/>";
This should work:
<script type="text/javascript">
var url = "/path/to/action?parameter1=<s:property value="omradeId">";
</script>
If not you should check if the value is not null and value is successfully set in your action class.
How do I pass a parameter from a page's useBean in JSP to a servlet in Java? I have some data in a form that gets passed no problem with a submit button, but no way to send anything else. Please help? Here is my code:
<input name = "deleteGameButton" type = "submit" value = "Delete"
onclick = "submitToServlet('DeleteGameServlet');">
Here is the corresponding javascript:
function submitToServlet(newAction)
{
document.userGameForm.action = newAction;
}
I'd like the servlet to have access to userBean
<jsp:useBean id = "userBean" scope = "session" class = "org.project.User" />
You kind of mess things here.
onclick() is Javascript and executed on client side. It has no (direct) way to update session-scoped bean. That bean is left on server-side, and was used when the HTML page was generated. To pass parameters back to servlet you need to use good old form fields, and submit the form.
Add more fields to the form, set their values before submit, then submit.
In Servlet call request.getParameter("name");
P.S. To automate this kind of things USE STRUTS. :-) Struts does exactly what you want: before passing the parameters to action, it populates the bean with those parameters. Transparently.
It depends exactly what you are trying to do. The
<jsp:useBean id = "userBean" scope = "session" class = "org.project.User" />
tag will allow you to use the userBean attribute of the session in your jsp. If there is not a userBean attribute in the session, it will create a new one (using the default constructor for org.project.User) and place it in the session.
Then, when you get to the servlet, you can retrieve it with:
User user = (User)request.getSession().getAttribute("userBean");
getServletConfig().getServletContext().getRequestDispatcher("servlet path & name");
dispatcher.forward (request, response);
Hi try with the next tag:
<jsp:useBean id = "userBean" scope = "session" class = "org.project.User"/>
<jsp:setProperty name="beanName" property="propertyname" value="value"/>
</jsp:useBean>
more here
Good luck!