In the previous article, we briefly understood the struts principles and learned SSH. The first part is the silly learning method. It's okay if people do what they do. Take logon as an example. The Struts configuration steps are summarized as follows (2.1 ):
Fig 2.2
1. Copy the jar package
The first is to create a java web project, and then open the strtus framework we downloaded, Struts-1.2.9-bin folder and struts-1.2.9.src source file folder. Copy the struts jar package to the struts_login-> lib folder (2.2) in the lib file in the bin folder ).
Fig 2.2
2,
Web. xml file configuration
Find the web under Struts-1.2.9-bin \ webapps \ Struts-blank \ WEB-INF in struts-1.2.9-bin --> webapps struts instance struts-blank in struts-1.2.9-bin --> webapps. xml file, copy the configuration of ActionServlet configuration, paste to our project struts_login under the WEB-INF web. in xml, the Code is as follows. Configure the ActionServlet that comes with struts.
action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml
debug
2
detail
2
2
action
*.do
3. Create your own ActionForm in the project
Create your own ActionForm in the project and inherit the ActionForm that has been written in the struts framework. The data used in ActionForm settings must be the same as the name set on our interface. Because when we submit a form, all the requests will be placed in the ActionForm. Create an ActionForm for Logon. The LoginActionForm. java code is as follows.
Package com. bjpowernode. struts; import org. apache. struts. action. actionForm;/*** log on to ActionForm to collect data from the form. * The form attributes must be consistent with the get and set attributes in ActionForm. * @ author summer **/public classLoginActionForm extends ActionForm {// user name. Private Stringusername; // password. Private String password; // set the password. Public voidsetPassword (Stringpassword) {this. password = password;} // obtain the user name. Public StringgetUsername () {return username;} // set the user name. Public voidsetUsername (Stringusername) {this. username = username ;}// get the password. Public StringgetPassword () {return password ;}}
4. Create your own Action
Create your own Action, inherit from org. apache. struts. action. Action in the struts framework, and reload the execute method of the parent class. Here the data in the form is retrieved. Use CalActionFormcalForm = (CalActionForm) form; (the value in the form can be obtained after we encapsulate it in the struts framework. After judgment, perform corresponding operations to jump to the corresponding page. The Action function is used to redirect pages after obtaining form data and calling business logic. Create the login Action class, the LoginAction. java class, and call the login method of the business logic class UserManager. The Code is as follows.
Packagecom. bjpowernode. struts; importjavax. servlet. http. httpServletRequest; importjavax. servlet. http. httpServletResponse; importorg. apache. struts. action. action; importorg. apache. struts. action. actionForm; importorg. apache. struts. action. actionForward; importorg. apache. struts. action. actionMapping;/*** log on to the Action * to obtain the form data, call the business logic, and return the redirection information. ** @ author summer **/public classLoginAction extendsAction {@ Override public ActionForward execute (ActionMappingmapping, ActionForm form, HttpServletRequest request, response) throws Exception {LoginActionForm laf = (LoginActionForm; stringusername = laf. getUsername (); Stringpassword = laf. getPassword (); UserManager userManager = newUserManager (); // pass the username and password try {userManager. login (username, password); request. setAttribute ("username", username); return mapping. findForward ("success");} catch (UserNotFoundException e) {e. printStackTrace (); request. setAttribute ("msg", "user not found, user name = [" + username + "+]");} catch (PasswordErrorException e) {e. printStackTrace (); request. setAttribute ("msg", "Incorrect password");} return mapping. findForward ("error ");}}
5. Establish
Struts-config.xml
As the core description of Struts framework, struts-config.xml can be said to "everything in control ". It not only describes the MVC model, defines the interfaces between all view layers and control layers (ActionForm), and combines them with the interfaces (Actions) at the control layer and model layer, you can also define additional components, such as international information resources to files and tag library information.
Still standing on the shoulders of giants, copy the struts-config.xml files in the struts bin folder we downloaded to the WEB-INF of our project and delete the comments section in the struts-config.xml. Configure Action and ActionForm. Put ActionForm
Login_success.jsp.
<% @ Page language = "java" contentType = "text/html; charset = GB18030" pageEncoding = "GB18030" %>
Insert title here$ {Username}: logon successful!
Login_error.jsp interface.
<%@page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
Insert title here <%-- <%=request.getAttribute("msg") %> --%> ${msg }
In this way, the struts framework is used to complete user logon. In this way, from initial learning to simple applications, as the number of applications increases, we will have a deeper understanding of struts and feel the convenience it brings to us.
Next Struts form processor ActionForm (static dynamic)