A simple login case of Struts1 entry of SSH learning.

The last article set up the blog environment.

This blog will start the basic learning of Struts1.


1. First understand the workflow of Struts1:

After the server starts, load ActionServlet according to web.xml to read the content of struts-config.xml file to memory

Take login as an example: the first time you enter login.jsp, Form will be instantiated and default values ​​will be assigned to form elements.
Enter the username and password to submit the form, submit it to the login.action of the action attribute, read the struts-config.xml file through ActionServlet to find the path attribute under action to find .action, and find the name attribute of form-beans in form-beans through the name attribute Get the package name and class name of the ActionForm, first instantiate the form, fill the form's value into the form, call the validate method of the form to verify, ActionErrors returns null to indicate that the verification is passed, otherwise it fails to return to the page specified by input. If the verification passes, the Action will be instantiated. Execute the execute method of Action.


2. Action, ActionForm, ActionForward, these three objects form the core of Struts.

 The core controller of Struts is ActionServlet, which intercepts user requests and transfers user requests to the Struts system.


3. Add configuration in web.xml:

<servlet>
		<!-- ActionServlet 的名 -->
		<servlet-name>action</servlet-name>
		<!-- 配置Servlet 的实现类 -->
		<servlet-class>
			org.apache.struts.action.ActionServlet
		</servlet-class>
		<!-- 指定Struts 的第一个配置文件 -->
		<init-param>
			<!-- 指定配置文件的映射 -->
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>

		<init-param>
			<param-name>debug</param-name>
			<param-value>2</param-value>
		</init-param>
		<init-param>
			<param-name>detail</param-name>
			<param-value>2</param-value>
		</init-param>

		<!-- 将ActionServlet配置成自启动Servlet -->
		<load-on-startup>2</load-on-startup>

	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

4. Implement and configure ActionForm:

ActionForm must inherit the ActionForm base class provided by Struts. This class is just a simple JavaBean containing the get and set methods of each property.

*The attribute name must be the same as the name in the form.

①Realization:

@SuppressWarnings("serial")
public class LoginActionForm extends ActionForm{

	private String userName;
	private String pswd;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPswd() {
		return pswd;
	}
	public void setPswd(String pswd) {
		this.pswd = pswd;
	}
	
}

②Configuration: Create the struts-config.xml configuration file specified in the web.xml file and add the <form-beans> attribute

<struts-config>
	<form-beans>
		<!-- 定义ActionForm,至少指定两个属性: name(自定义) , type LoginActionForm的路径 -->
		<form-bean name="loginActionForm" type="com.example.LoginActionForm" />
	</form-beans>
</struts-config>


5. Implement and configure Action:

Inherit the Action base class provided by struts, implement the execute method of ActionFoward type, and return an ActionMapping forward url.

Action is not thread safe because all requests share an action instance.


①Realization:

public class LoginAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginActionForm lForm = (LoginActionForm) form;
		String userName = lForm.getUserName();
		String pswd = lForm.getPswd();
		if (userName.equals("") || pswd.equals("") || pswd == null
				|| userName == null) {
			userName = "admin";
			pswd = "admin";
		}
		if ("admin".equals(userName) && "admin".equals(pswd)) {
			return mapping.findForward("welcome");
		}

		return mapping.findForward("error");

	}
}
②Configuration:

<struts-config>
	
	<!-- path:定义的路径;type:Action的路径;name:<form-beans>中定义的name,scope范围 -->
	<action-mappings>
		<action path="/login" type="com.example.LoginAction" name="loginActionForm"
			scope="request">
			<!-- 配置局部Forward -->
			<forward name="welcome" path="/index.jsp" />
			<forward name="input" path="/login.jsp" />
		</action>
		
	</action-mappings>
</struts-config>

6. Configure forward, including two types of global forward and local forward. The local is as shown above, and the name is the url that execute will forward.

redirect: Control whether to forward or redirect. Default false forwarding: RequestDispatch.forward , true redirection: HttpServletResponse.sendRedirects
global forward:

<!--配置全局Forward对象 -->
	<global-forwards>
		<!--该Forward对象的name 属性为error. 映射资源为/WEB-INF/jsp/error.jsp -->
		<forward name="error" path="/error.jsp" />
	</global-forwards>

7. The entire struts-config.xml attributes:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC  
         "-//Apache Software Foundation//DTD Struts Configuration1.2//EN"  
         "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">


<struts-config>
	<form-beans>
		<!-- 定义ActionForm,至少指定两个属性: name(自定义) , type 路径 -->
		<form-bean name="loginActionForm" type="com.example.LoginActionForm" />
	</form-beans>
	<!--配置全局Forward对象 -->
	<global-forwards>
		<!--该Forward对象的name 属性为error. 映射资源为/WEB-INF/jsp/error.jsp -->
		<forward name="error" path="/error.jsp" />
	</global-forwards>
	<!-- path:定义的路径;type:Action的路径;name:<form-beans>中定义的name,scope范围 -->
	<action-mappings>
		<action path="/login" type="com.example.LoginAction" name="loginActionForm"
			scope="request">
			<!-- 配置局部Forward -->
			<forward name="welcome" path="/index.jsp" />
			<forward name="input" path="/login.jsp" />
		</action>
		
	</action-mappings>
</struts-config>

8.login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 	<form action="login.do" method="post">
 	 <p>用户名:</p><input name="userName" type="text"/><br>
 	 <p>密码:</p><input name="pswd" type="password"/> <br>
 	 <input type="submit" value="登陆"  />
 	</form>
</body>
</html>




Guess you like

Origin blog.csdn.net/u010857795/article/details/50956785