一。在WEB-INF下建立一个lib文件夹然后将包拷贝进来
二。配置web-xml文件 配置过滤器filter
<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>
三。创建login.jsp及success.jsp
login.jsp
<form action="login.action" method="post">
姓名:<input type="text" name="name" value="丽丽"> <br>
密码:<input type="password" name="password" value="123123"> <br>
<input type="submit" value="提交">
</form>
success.jsp
<body>
<h2>用户登录成功</h2>
账号:${name}<br>
密码:${password}<br>
</body>
四。创建LoginAaction.java类 继承于ActionSupport 并重写execute()方法
public class LoginAction extends ActionSupport {
private String name;
private String password;
@Override
public String execute() throws Exception {
System.out.println("账号" + name);
System.out.println("密码" + password);
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
五。配置struts.xml并拷贝到src目录下
<?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> -->
<action name="login" class="com.learn.action.LoginAction">
<result name="success">
/success.jsp
</result>
</action>
</package>
</struts>