Struts2 simple login example

1.struts1 uses ActionServlet to obtain user requests to act as a controller. The core is action, actionform, actionforward,

The core of struts2 is action, interceptor, action can not inherit the parent class, and it combines action and actionform to reduce coupling.

2. Still start from the simplest login, the project is still a maven project, the pilot package, not detailed, add the package dependency to pom.xml, and then build.

3. Web.xml configuration, no longer use servlet, but filter:

<!-- 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>
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>

4. The configuration of struts.xml is placed under resources:

Package attribute: ①name: required, package name.

                         ②Namespace: Optional, specify the namespace, you can allow different namespaces and action names with the same name. The default is "". If the namespace is filled in, when the action processes the URL, specify: namespace/action name. Such as: action="struts2Demo/login.action"

                         ③ extends: inherit the parent xml, including actions and interceptors defined by the parent class.

Action attribute: ①name: the custom action name to be used later.

                    ②class: Specify the class where the action is located

                    ③method: Optional, specify a specific method in action, so you don't need to cover the method in ActionSupport.

<span style="color:#000000;"> <struts>  
        <package name="struts2Demo" namespace="/struts2Demo" extends="struts-default">  
            <action name="login" class="struts2Demo.LoginAction">  
                <result name="suc">/index.jsp</result>  
                <result name="fail">/error.jsp</result>  
            </action>  
        </package>  
    </struts>  </span>
5. LoginAction can not inherit the parent class, but the method to be executed must have the same name as the method defined by the parent class, such as execute, or it can inherit ActionSupport and override the provided method.

<span style="color:#000000;">public class LoginAction {
	private String user;
	private String pswd;
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPswd() {
		return pswd;
	}
	public void setPswd(String pswd) {
		this.pswd = pswd;
	}
	public String exe(){
		if("admin".equals(user) && "admin".equals(pswd)) {
			return "suc";
		}
		return "fail";
		
	}
}</span>
6.login.jsp

<span style="color:#000000;"><form action="struts2Demo/login.action" method="post">
 	 <p>用户名:</p><input name="userName" type="text"/><br>
 	 <p>密码:</p><input name="pswd" type="password"/> <br>
 	 <input type="submit" value="登陆"  />
 	</form></span>




          

         

Guess you like

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