I have a common JSP included in other pages; it must generically display a certain list from a bean.
All the main JSPs use a certain subclass of that bean.
In struts-config.xml, let's say there is one specific definition for my Action Form bean as an example,
<form-bean name="rr5YearBudgetForm" type="myapp.form.My5YearBudgetActionForm" />
The common JSP needs to check properties of that bean, in a general way (with the bean I pass in); these are properties which all the form beans will support. Right now, it's hard-coded to use the specific Form Bean below, but it must work with any bean that's passed in.
<c:if
test="${rr5YearBudgetForm.saved == true || fn:length(rr5YearBudgetForm.budgetPeriods) > 1}">
The question is, how do I pass in a specific variable containing my form-bean? I tried this simple c:set var, but that defines a string, with no properties.
Calling JSP
<c:set var="budgetForm" value="rr5YearBudgetForm" scope="request" />
<%# include file="common.jsp" %>
Common JSP
<c:if test="${budgetForm.saved == true || fn:length(budgetForm.budgetPeriods) > 1}">
but that does not work. I need a way to pass in the actual bean rather than a string.
Please try the bean tag:
<s:bean name="com.mkyong.common.action.HelloBean" var="hello">
<s:param name="msg">Hello Bean Tag</s:param>
</s:bean>
This tag library contains tags useful in accessing beans and their properties, as well as defining new beans (based on these accesses) that are accessible to the remainder of the page via scripting variables and page scope attributes.
Related
I would like to set a value of bean from another bean once a page is displayed. The target dataTable is included because it is also used in another context.
Like I said before, there are two beans involved:
bean1 'gets' a parameter, will load an object and display some properties.
bean2 is the bean that is responsible for the filtering / search in the dataTable.
relevant parts:
<f:metadata>
<f:viewParam name="objectId" value="#{bean1.objectId}" />
</f:metadata>
<!-- resolving works -->
#{bean1.object.name}
included search:
<p:dataTable>...<p:inputText value="#{bean2.value}">...</p:dataTable>
How to assign (a substring of) bean1.object.name to the value of the input text once at page request but keep the existing value attribute of the field? I don't want to mess up the included page but would prefer to solve it "outside" in my including jsf/xhtml file.
The best way to do this is using javascript.
Define a input hidden tag to hold the value of #{bean1.object.name}.
Now in javascript get the value of this field using document.getElementById("").value
Take the substring and assign to input text field using javascript.
I have to send object of Bean class back to JSP from my action class. I am instantiating the Bean class in my action class and and setting some values in my Action class.
ElasticitiesVariable elasticitiesVariable = new ElasticitiesVariable();
elasticitiesVariable.setAuto_Auto_cost_peak("-0.047");
elasticitiesVariable.setAuto_Ride_cost_peak("0.000");
elasticitiesVariable.setAuto_Van_cost_peak("0.000");
elasticitiesVariable.setAuto_Transit_cost_peak("0.050");
elasticitiesVariable.setAuto_Bike_cost_peak("0.000");
and in my JSP I am declaring the bean class as well.
<s:bean name="org.apache.struts.register.model.ElasticitiesVariable" var="elasticitiesBean" />
<td class="edit_area">
<s:property value="#elasticitiesBean.auto_Auto_cost_peak" /></td>
in the bean class i have declared the variable private with public getter and setter.
But the values coming null in JSP.
After debugging I found that there is no problem in action class. But when command goes to bean class, values becomes null.
First time you instantiate the bean in the action, second time in the JSP. It's not the same bean you've populated in the action, and doesn't contain the values. If you want to get the values from the first bean then you should create mutators to a variable that hold the instance of the bean. You should also create mutators to access the properties of that bean. In the JSP you have access to the first bean because the action instance is on the top of the valueStack. Just use
<s:property value="elasticitiesBean.auto_Auto_cost_peak" />
Note, that without # OGNL will not search the other variables, it will look directly into the valueStack. But don't do it inside the bean tag, because the new (second) instance is pushed to the valueStack and the search is performed from top to bottom, thus it could match the properties of the bean. Just remove the bean tag.
I want to know if we populate action class properties, can we access them directly in the result JSP without assigning them to a form in the action class?
I think you need to have the bean getter method defined properly . Say , if a property is named id and you have a method called getId() in your action. Then it can be accessed directly in the JSP as :
OGNL expression:
<s:property value="id"/>
OR
JSTL expression:
<c:out value="${action.id}"/>
one thing u can do is that initialize the properties in the ModelDriven Class which u want to be intialized whenever your action is called.
http://struts.apache.org/release/2.1.x/docs/model-driven.html
Struts2/XWork1 and its taglib is oriented towards OGNL, which is using
a valueStack for all action properties. These values are not direct
available for the expression language of JSP2.0/JSTL1.1.
To access it via OGNL you can use OGNL only in Struts tag attributes. For example
public String MyAction extends ActionSupport {
private String myAttr;
//getter and setter here
...
<s:set var="myAttrz" value="myAttr" scope="request"/>
${myAttrz} or ${myAttr} will just print out the value.
<%=request.getAttribute("myAttrz")%>
In the first tag the action attribute is accessed as OGNL expression
In the second line as JSTL expression.
In the third line scriptlets.
References:
Using Struts and XWork with JSP 2.0 and JSTL 1.1
Application, Session, Request objects in JSP
Can we use JSTL with the framework
Is it possible to access fields defined in Beans/Forms used within an action from a JSP page?
At the moment I can use this:
<s:action name="actionName" var="foo" executeResult="false" />
and access any fields defined in that action class using
<s:property value="#foo.bar" />
but this doesn't seem to work for values defined in a bean or form, which I would normally be able to access using the property tag in the result JSP page for an action.
Just tested this with Struts 2 version 2.3.4.1 and it worked for me to get the displayName value of the kuPerson bean. Note in my Struts action class I have a getKuPerson method that returns an object of type KuPerson and that KuPerson class has a getDisplayName method that returns a String.
<s:action name="person" var="personAction" executeResult="false" />
Display name: <s:property value="#personAction.kuPerson.displayName" />
I have a HashMap<Long, ClientProperties> that I'm putting on the ServletContext at startup.
//During application-startup:
//getProperties() returns HashMap<Long, ClientProperties>
context.setAttribute("clientProps", clientManager.getProperties());
ClientProperties is a POJO with 5 attributes that i need to access in my jsp.
Basicly I need to get the correct POJO (by HashMap-key) and access its properties in my jsp.
More spesific (for example purposes), one of the POJO attributes is clientLogo
In my jsp i now have:
<img src="<c:url value='/images/logo.png'/>" alt="Logo">
I need to replace the path to the logo-file with the clientLogo-property of the POJO.
The HashMap-key to use should be extracted from the User-object stored in the session. It can be retrieved like this: ${sessionScope['user'].clientId}
Any ideas?
Using struts2 and spring btw if that matters.
To get an attribute foo from the servlet context, you use the same syntax as to get it from the session, but replace sessionScope by applicationScope.
But you have so many nested things here that you should define variables:
<c:set var="map" value="${applicationScope['clientProps']}"/>
<c:set var="mapKey" value="${sessionScope['user'].clientId}"/>
<c:set var="pojo" value="${map[mapKey]}"/>
<c:set var="clientLogo" value="${pojo.clientLogo}"/>
<c:url value="${clientLogo}"/>
Note that this is typically the kind of hard work that you should not have to do in the view. Implement the retrieval of the image path in the controller, in Java, and make it available as a property of your action/form, and access it directly from your view.