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
Related
If I have a
ArrayList<Person> persons
How do I access it in EL?
<c:foreach items="${what goes here??}" var="person">${person.title}</c:foreach>
The expression ${foo} uses behind the scenes JspContext#findAttribute() which searches for attributes in PageContext, HttpServletRequest, HttpSession and ServletContext in this order by their getAttribute("foo") method whereby foo from ${foo} thus represents the attribute name "foo" and returns the first non-null object.
So, if you do in a servlet
ArrayList<Person> persons = getItSomehow();
request.setAttribute("persons", persons); // It's now available by ${persons}
request.getRequestDispatcher("/WEB-INF/persons.jsp").forward(request, response);
And call this servlet by URL, then you'll be able to iterate over it in page.jsp as follows:
<c:foreach items="${persons}" var="person">
${person.title}
<c:forEach>
The above is also equally valid when you put it in the session scope instead
request.getSession().setAttribute("persons", persons);
or even in the application scope
getServletContext().setAttribute("persons", persons);
EL will for title in ${person.title} implicitly look for a public instance (not static!) method prefixed with get in Person class like below:
public String getTitle() {
return title;
}
The field title does not necessarily need to exist in the class (so you can even return a hardcoded string and keep using ${person.title}), and it does not necessarily need to be an instance field (so it can also be a static field, as long as the getter method itself isn't static).
Only boolean (not Boolean!) getters have a special treatment; EL will implicitly look for a public method prefixed with is. E.g. for a ${person.awesome}:
public boolean isAwesome() {
return awesome;
}
See also:
Our EL wiki page
How do servlets work? Instantiation, sessions, shared variables and multithreading
How to avoid Java code in JSP files?
Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
Use EL ${XY} directly in scriptlet <% XY %>
How does Java expression language resolve boolean attributes? (in JSF 1.2)
<c:forEach var="item" items="${names}"> ${item.title} </c:forEach>
names should be in the set as attribute available for the view
If you are using Servlets or action class for creating your list and then forwarding it to your JSP, then you must be having following line in your servlet or action class.
ArrayList<Person> names = "get from somewhere";
request.setAttribute("personNames",names);
<c:foreach var="item" items="${personNames}"> ${item.title} </c:foreach>
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.
I am trying to access the value of the variable defined in the method in the action class in jsp using Struts2.
I have a variable defined desc_map in the method of the action class.I want to access the value of this in jsp.
How can I do it?
Thank you.
Create a public getter for the property, preferably named correctly as per the JavaBean spec:
public TheType getDescMap() {
return desc_map;
}
Access it from the JSP:
${descMap}
Or if you need escaping, can't use JSP 2.0+, etc:
<s:property value="descMap"/>
All of this is covered in essentially every Struts 2 tutorial.
I have a serveresource method which is invoked on clicking a link. The serveresource method takes the input from the parameter that was passed and retrieves a row from the database. Now all the values in a row are in set using the mutator methods. I have everything in a java object. I need to pass this object to a jsp page to print the values of a single row on the jsp page. I am not sure how to handle that java object in the jsp page instead of setting each value as an attribute in the serveresource method. Need help from experts.. Thanks in Advance
UPDATE
It was because I have an Ajax call and when I set values it is in a completely different life cycle which is causing the problem. I figured it out.
You should defining the java object as Bean in JSP.
The Bean in JSP can be defined using < jsp:useBean..> standard jsp tag. And set and get property using < jsp:setProperty..> and < jsp:getProperty..> standard jsp tags.
Refernces:
Use Bean
Using Beans in JSP. A brief introduction to JSP and Java Beans
UseBean syntax
< jsp:setProperty> and < jsp:getPropety> syntax
The usual method is to add it to the HttpServletRequest object, thus:
MyBean myBean = new MyBean();
myBean.setValue("something);
myBean.setAnotherValue("something else");
// ... stuff ...
request.setAttribute("myBean", MyBean);
This can be accessed from the jsp page using EL thus:
<table>
<tr>
<td>${myBean.value}</td>
<td>${myBean.anotherValue}</td>
</tr>
</table>
you can bind with request object
In Servlet or JSP
request.setAttribute("strIdentifire", yourJavaObject);
In JSP
YourJavaObjectClass obj = (YourJavaObjectClass)request.getAttribute("strIdentifire");
How to declare a variable and assign value to that variable in Struts2?
Use the set tag:
<s:set var="myVar">hello</s:set>
read the var with:
<s:property value="#myVar"/>
Another example:
<s:set name="personName" value="person.name"/>
Hello, <s:property value="#personName"/>. How are you?
where person is a bean in your value stack
You shouldn't have to declare variables in JSPs. Do it in your Action and create a getter so you can access it from the JSP showing the action's output.
Well in struts2 we have concept of value stack and during request processing Struts2 framework will push the action on the top of value stack and its properties (Variable) will work as they are on the top of it.
all you need to have getter and setter for your variable and you can than access values both (in/out) in struts2 with OGNL.OGNL is an expression language integrated with Struts2 which is capable of refereeing values from the value stack and also will do the data conversion (except custom type) for you