1.拷貝所需jar到WEB工程目錄下的lib
<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>
3.編寫login.jsp:
<form action="login.action" method="post">
<h1>用戶登錄</h1>
賬號:<input type="text" name="userName"/><br/>
密碼:<input type="text" name="password"><br/>
<input type="submit" value="登錄"/>
</form>
4.創建LoginAction類繼承ActionSupport類,重寫execute()
package com.learn.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String userName;
private String password;
@Override
public String execute() throws Exception {
System.out.println("賬號:"+userName);
System.out.println("密碼:"+password);
return "success";
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<?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">
<struts>
<!-- 動態方法調用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!-- 開發模式 -->
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.learn.action.LoginAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
6、編寫success.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>
<h1>用戶登陸成功</h1>
賬號:${userName}<br>
密碼:${password}<br>
</body>
</html>