The first day of the new Year, for Programer, compared to the usual is not much different, the code is still knocking. Do a day of keyboard, not necessarily have any brilliant achievements (in fact, it seems to have been this/(ㄒoㄒ)/~~), but was once stuck in the problem again stumbled and then tangled to solve, by the way to review a bit. The problem is that the Struts2 tags and JSP's Java code directly implement the variables to reference each other.
Scenario 1: There is a variable in Valuestack, and our goal may change its value in addition to this value, which may be done through Webservice,service, DAO, etc., which are implemented in Java encoding. For simplicity's sake, it's just inside the Java code or the variable, and changing its value. Borrow the <s:set/> tag and the JSP's built-in object PageContext, as follows:
First define the action
Public classMyactionextendsActionsupportImplementsServletrequestaware, Servletresponseaware {httpservletrequest httpservletrequest; HttpServletResponse HttpServletResponse;
Defines the variable String valueinvaluestack in the Valuestack; Omit setter and Getter PublicString Execute ()throwsException {Try {
Valueinvaluestack= "Default value in Valuestack"; System.out.println ("Valueinvaluestack =" +valueinvaluestack); Output: Valueinvaluestack = default value in ValuestackreturnSUCCESS; } Catch(Exception e) {e.printstacktrace (System.out); returnERROR; } }}
Configure the action inside the Struts.xml
< Packagename= "Test"extends= "Struts-default"namespace="/">Define the Struts2 action Myaction<Actionname= "Myaction"class= "Com.test.MyAction"Method= "Execute"> <resultname= "Success">/jsp/result.jsp</result> </Action> </ Package>
Defines Myaction's execute returned page result.js<%@ pages contenttype= "Text/html;charset=utf-8" language= "java"%<%@ taglib Prefix= "S" uri= "/struts-tags"%
the value originally in the Valuestack:<s:property value= "Valueinvaluestack"/>
//Use <s:set/> to define a variable called newvariable, and its value is taken from Valuestack<s:set var= "newvariable" scope= "page" value= "Valueinvaluestack"/> </p> <%
//PageContext out the variable in the JSP's built-in object
String S= (String) pagecontext.getattribute ("Newvariable"); Out.println ("Values for embedded Java code:" +s); S= "The newvariable, whose value is changed from Valuestack"; after the operation is completed, the variable is placed inside the PageContext again, and the name remains unchanged. If you want to get the variable in the request or session range, you need to put it in the corresponding range.
Pagecontext.setattribute ("Newvariable", s); %> <p>
//by OGNL attr to get the value of the variable placed in PageContext : <s:property value= "%{#attr. newvariable}"/> </p></body>
The actual result is as follows:
In this process, the Pagecontext,pagecontext object is an instance of the Javax.servlet.jsp.PageContext class that is used to represent the entire JSP page. This object is primarily used to access page information while filtering out most of the implementation details. This object stores a reference to the request object and the response object. Application objects, config objects, session objects, and out objects can be exported by accessing the properties of this object.
STRUTS2 tags and JSP embedded Java code variables are referenced to each other