The HelloAction class and view components share data within the request () and getAttribute () methods between HTTPSERVLETREQUEST. Let's take a look at the details of the HTTPSERVLETREQUEST's setAttribute () method. When the HTTPServletRequest's SetAttribute () method is transferred to Hello.jsp, you need to provide a property named "Personbean" key: Request.SetaTRibute ("Personbean", PB); hello.jsp passes this name "Personbean" properties Key to read the Personbean object:
Hello
h2>
logic: present>
For Struts applications, it is advocated to define these properties key constants in a Java file constants.java, routines 2-6 show its source program.
Routines 2-6 Constants.java
Package hello;
Public Final Class Constants {
/ **
* The Application Scope Attribute Under Which Our User Database
* is store
* /
Public static final string person_key = "personbean";
}
In this way, the HelloAction class can call HTTPSERVLETREQUEST's setAttribute () method: request.setattribute (constants.person_key, pb); definition some constant can improve the independence of the Action class in Constants.java, when the property KEY constant value If you change, you only need to modify the constants.java file and do not need to modify the Action class. In addition, in this example, the PersonBean object is saved in the HTTPServletRequest object. For other actual web applications, you can also save the JavaBean object in the httpsession object as needed. This article is selected from the Freescale Book "Self-passs: MVC-based Java Web Design and Development"