3. Use annotations in the action class to establish the relationship between the action and the webpage.
We entered the last phase of annotation-based struts2.0 + hibernate3.3 + spring2.5 integrated development. This phase mainly introduced struts2 annotation for our project. Compared with the integration of Hibernate with annotation and spring with annotation, it is easier to integrate Struts with spring with annotation.
Let's first introduce struts2.0 related jar package: xwork-2.0.5.jar, struts2-core-2.0.11.2.jar, ognl-2.6.11.jar, commons-logging.jar (this front has been introduced), freemarker-2.3.8.jar, and struts2.0 and spring integration required struts2-spring-plugin-2.0.11.2.jar. Well, we will develop a small example of user registration immediately.
Some code on the registration page of register. jsp is as follows:
<S: Form Action = "register">
<S: textfield name = "username"/>
<S: password name = "password"/>
<S: Submit value = "register"/>
</S: Form>
<S: fielderror> </S: fielderror>
The Code on the successful registration page of success. jsp is as follows:
Registered successfully! Hello, <s: property value = "username"/>!
Well, let's develop our control layer. Write a registeraction. Java class: Package com. Rong. Action;
Import javax. annotation. resource;
Import org. Apache. struts2.config. parentpackage;
Import org. Apache. struts2.config. result;
Import org. Apache. struts2.config. results;
Import org. springframework. Context. annotation. scope;
Import org. springframework. stereotype. Controller;
Import com. opensymphony. xwork2.actionsupport;
Import com. opensymphony. xwork2.validator. Annotations. requiredstringvalidator;
Import com. Rong. entity. user;
Import com. Rong. Service. userservice;
@ Controller @ scope ("prototype") // declare this class as a control layer class and call it in prototype mode
@ Parentpackage (value = "struts-Default ")
@ Results ({
@ Result (name = "success", value = "success. jsp "),
@ Result (name = "input", value = "register. jsp ")
})
Public class registeraction extends actionsupport {
@ Resource (name = "userservice ")
Private userservice;
Private string username;
Private string password;
@ Override
Public String execute () throws exception {
User user = new user ();
User. setusername ("rongxinhua ");
User. setpassword ("blogjava ");
Userservice. Save (User );
Return success;
}
Public String GetUserName (){
Return username;
}
@ Requiredstringvalidator (Message = "Enter the user name! ")
Public void setusername (string username ){
This. Username = username;
}
Public String GetPassword (){
Return password;
}
@ Requiredstringvalidator (Message = "enter the password! ")
Public void setpassword (string password ){
This. Password = password;
}
}
Spring2.5 uses annotations such as @ controller @ scope ("prototype") to assign struts actions to their own control scope. Annotations such as @ parentpackage and @ results provided by struts2.0 indicate the parent package to be inherited and the response result. There are also annotations such as @ requiredstringvalidator and @ requiredfieldvalidator, which greatly facilitates the verification of form information. Have you found anything missing? We do not need struts. xml configuration file, do not need xxx-validation.xml, do not need to write complicated verification code. Isn't it very convenient?
But don't forget, we have to configure struts2 in Web. xml: <! -- Configure struts 2.0 -->
<Filter>
<Filter-Name> struts2 </filter-Name>
<Filter-class> org. Apache. struts2.dispatcher. filterdispatcher </filter-class>
<Init-param>
<Param-Name> actionpackages </param-Name>
<Param-value> com. Rong. Action </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> struts2 </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
We init a parameter actionpackages, indicating where our request looks for our action. For example, if our user registers action = "register", it searches for the registeraction class under the com. Rong. action package by default. After Action processing, if success is returned, it will jump to success. jsp. If the verification fails, the input is returned, and the request is forwarded back to register. jsp.
Okay. Check the running effect!
Success! Our annotation-based SSH Integrated Development has finally been completed. There are not many annotation annotations I have introduced. You have to find the materials to learn. Our focus is on how the three frameworks are integrated when annotation is used.
Let's talk about the problem. Many people have asked this question when annotation or XML is used during the development process. A teacher from a training institution said that annotation can be used, but xml configuration must be understood, because xml configuration is still mainstream. He also makes sense. Some people may not accept annotation, just as I didn't like it very much. One of my old Java middleware instructors said, "What technology does struts, spring, and hibernate count? Learning EJB is useful ." He taught us the ejb2 version. He used JBuilder to develop EJB projects when he was young. If I asked him "why didn't he choose ejb3.0", would he say: "ejb3 was changed to a mess, what annotations? It's still the best of ejb2 ......". Of course, I didn't ask him again. Sometimes, new things are accepted by people. It takes some time for the world to change and knowledge to change. Why don't we accept new things with a broader mindset?