问题
I want to know if we populates action class properties, can we access them directly in the result JSP with out assign them to a form in the action class?
回答1:
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}"/>
回答2:
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
回答3:
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
来源:https://stackoverflow.com/questions/17133737/access-struts-2-action-class-properties-directly