Readbook:struts2
Let's talk about Struts1:
STRUTS1 is developed using Model II mode, which is Jsp+java Bean+servlet
And again its flaws:
1. The presentation layer supports a single, originally in the Java Web Application World, the presentation Layer technology choice does not necessarily have to choose the JSP, other view layer can be used to replace the choice of technology such as Freemaker,velocity,
But struts 1 cannot be integrated with it.
2. Over-reliance on the Servlet API
The Servlet API requires the Web container to initialize, and once it is out of the Web container, it is difficult to complete the test.
3. Not conducive to code reuse
Some of the classes in Struts 1, such as Actionform, are coupled with code that is difficult to reuse in other systems.
So struts2 changed his mind and absorbed some ideas from struts 1, and the framework was reconstructed with the design thought of WebWork (another similar framework project in Java).
Official website: http://struts.apache.org/
Base Package struts2-core-xxx.jar struts2 Framework Core class library xwork-core-xxx.jar xwork class Library, Struts2 Building the base ognl-xx.jar struts2 An Expression language class library freemaker-Xxx.jar STRUTS2 's label template uses the class library javassist-xxx. Ga.jar byte code processing Commons-fileupload-xx.jar file upload Commons-io-Xx.jar Java IO Extensions Commons-lang-Xx.jar contains some of the data types of the tool library Commons-lang3-xx.jar
If the use of some non-basic class library class causes the console error notification class Library is missing, can be based on the information into the framework of other corresponding class library
Struts HelloWorld:
About Struts Ognl (object map navigation language) Expression language
1. JSP<%@ page language= "Java"Import= "java.util.*" pageencoding= "UTF-8"%><% @taglib uri= "/struts-tags" prefix= "s"%> <%--Comment: Add this line to the JSP file--% > ... <%--output the value of message in struts action--%> class= "Row" > <form action= "helloworld.action" method= "POST" > <input name= "name" Type= "Tex T "/> <input type=" Submit "value=" commit "/> </form> </div> </BODY&G T;. Java class write handle action logic code Packagecom.bud.test;Importcom.opensymphony.xwork2.Action; Public classHelloworldactionImplementsAction {PrivateString name = ""; PrivateString message = ""; PublicString Execute ()throwsException {//TODO auto-generated Method Stub This. Setmessage ("Hello," + This. GetName () + "!"); return"Success"; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString getMessage () {returnmessage; } Public voidsetmessage (String message) { This. Message =message; }}
What happens when the form commits, struts2 to convert the request parameters,
At the same time, according to the idea of the configuration, directly to the action in the same name Action Class property assignment (through setter method)
Excute () return five constants for action
Success indicates that the program is handling normally and returns a successful result of the user none means that the processing ends normally, but does not return any data from the User Client error indicates that the processing result failed input Indicates that more user input is required to successfully execute login indicating that the user is required to log in correctly before it can be executed successfully
The above 5 constants are the name of the action default support logical view, also support extension return other specific string as logical view name
3.web.xml configured to intercept all requests to struts2
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class></filter ><filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/* </url-pattern></filter-mapping>notice: Here org.xxx is based on the class in the jar of the specific struts2 core.
4.struts2 configuration file
Struts.xml under src path
because of a problem with different versions of XML document definitions I got the corresponding version of struts in its sample app to copy this profile modification
However, if you use MyEclipse, you can use the project on the right-click menu Myeclipse-->add Struts capabilities ...
However, the principle is not used to create a tool here
I found that the regular document definition will only change the Configuration 2.3 dtds/struts-2.3.dtd here the second version number, such as 2.0, 2.3 and so on
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public " -//apache software foundation//dtd Struts Configuration 2.3//en " http://struts.apache.org/dtds/struts-2.3.dtd" ><!--START snippet:xworksample--><struts> <!--Create default package, inherit Struts-default package-- < Package extends= "Struts-default" > class= "Com.bud.test.HelloWorldAction" > <result Name= "Success" >/test/hello-world.jsp</result> </action> </Package > </struts><!--END snippet:xworksample--Notice: Note the case for each step in each step
Node description of the configuration file:
The package element defines the logical unit &NBSP for STRUTS2 request processing; the
Name property must be unique (the name of the specified packet, which can be used for other package references)
The extends property is used to specify which package inherits from
namespace to specify a namespace, so that an action with the same name can appear logically under different namespaces
The action element used to configure the action class
name property is the name of the action
class is used to set the fully qualified name class for action processing
result is used to set what the system will do next after the action is processed
name property is a logical view name and must match the action class return value
The value of the result node is the view position of the corresponding result
actually more JavaBean (entity object) is used to hold the information
then the action and JSP become this way
Action: Public class Implements action{ private user user; Public throws Exception { //... } ... // Getter Setter ... }jsp:<input type= "text" name= "User.Name"/><input type= "password" name= "User.password"/>
Struts2 will actually call Loginaction.getuser (). SetName ()
The Bean object is not instantiated in the original action code and should be error-free, but any objects used to populate the data are automatically instantiated in struts
Struts2 Accessing the Servlet API
How to access the Servlet API decoupling:
Using the Actioncontext class to get the map object corresponding to the Servlet API object, in the action class, we can directly access the map object of the HttpServletRequest HttpSession and Serveletcontext
Obtained through the Com.opensymphony.xwork2.ActionContext class within the Struts framework, Actioncontext is the context of the action execution, A set of objects required to execute the action class is saved in Actioncontext
Get method:
Actioncontext ac = actioncontext.getcontext (); // Get HttpServletRequest Map request = (map) ac.get ("request"); // Get HttpSession Map session = ac.getsession (); // Get ServletContext MAP application = Ac.getapplication ();
Instance:
JSP:class= "Row" > <form action= "login.action" method= "POST" > <input name= "user.name" type= "text" placeholder= " User name "/> <input name=" user.pass "type=" password "placeholder=" password "/> <br/> <input ty Pe= "Submit" value= "Submission"/> </form></div>Action: Public classLoginactionImplementsaction{PrivateString message = ""; Privateuser User; PublicString Execute ()throwsException { This. Setmessage ("Hello," + This. User.getname () + "! Pass: "+ This. User.getpass ()); Actioncontext AC=Actioncontext.getcontext (); Map<String,Object> session =ac.getsession (); if("Lili". Equals ( This. User.getname ()) && "dd". Equals ( This. User.getpass ())) {Session.put ("Current_User", This. User.getname ()); return"Success"; } Else{ return"Error"; } } PublicString getMessage () {returnmessage; } Public voidsetmessage (String message) { This. Message =message; } PublicUser GetUser () {returnuser; } Public voidsetUser (user user) { This. user =user; }}bean: Public classUser {PrivateString name = ""; PrivateString pass= ""; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString Getpass () {returnPass; } Public voidSetPass (String pass) { This. pass =Pass; Action configuration within the}}struts.xml:<action name= "Login"class= "Com.bud.test.LoginAction" > <result name= "Success" >/test/hello-world.jsp</result> <result name= "Error" >/index.jsp</result></action>
How to access the Servlet API coupling:
To get a Servlet API object directly, you can use the Org.apache.struts2.ServletActionContext class,
It is a subclass of Actioncontext that defines the following methods for getting Servlet API objects in this class
Public Static httpservletrequest getrequest (): Get HttpServletRequest Object Public Static ServletContext getservletcontext (): Get ServletContext object Public Static Httpservletreponse getResponse (): Get HttpServletResponse Object
Notice
The Servletactioncontext object does not define a way to get the HttpSession object directly, but you can get it by httpservletrequest
You can also implement a specific interface through the action class, injecting the Servlet API object into the action instance by struts2
voidvoid void Setservletresponse (HttpServletResponse reponse)
Follow up to add ...
Struts2 Finishing for Java