1.在eclipse、myeclipse中创建一个web project;
2.在lib中添加必要的jar包(缺一不可)
struts-core-2.**.jar xwork-2.**.jar; commons-fileupload-**.jar;
commons-logging-**. jar; ognl-**.jar; freemarker-**.jar;
3.在web中添加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>*.action</url-pattern>
</filter-mapping> <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>*.action</url-pattern>
</filter-mapping>
4.编写action类,LoginAction
package com.action;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action {
private String username;
private String password;
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;
}
public String execute(){
if(username.equals("admin")&&password.equals("888888")){
return "success";
}
return "success";
}
}
5.进行添加struts.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="book" extends="struts-default">
<action name="login" class="com.action.LoginAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
6.进行添加jsp页面 login.jsp success.jsp(注意:页面中的属性元素要与Action所需要的属性元素一致)
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="gbk"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="login.action" method="post">
用户名:<input name="username"/><br>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
7.进行添加tomcat服务,然后进行启动项目,进行访问http://localhost:8080/MyStruts/login.jsp即可。