一、建立一個j2ee工程
二、在WebContent-WEB-INT-lib中導入以下jar包
三、在WebContent-WEB-INT-web.xml中加入以下內容
<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文件
a、 在頂部添加
<%@ taglib prefix= "s" uri = "/struts-tags" %>
b、body中添加
<s:form action= "login">
<s:textfield name ="username" key ="用戶名" ></s:textfield >
<s:textfield name ="password" key ="密碼" ></s:textfield >
<s:submit ></s:submit >
</s:form >
五、在java Resources-src下建立demo包,在其中建立一個LoginAction.java
package demo;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
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 () throws Exception{
if( username. equals( password)){
return "success" ;
} else{
return "error" ;
}
}
}
6 、在java Resources-src中建立struts.xml
七、建立welcome.jsp和error.jsp,內容自定義,分別表示登陸成功和登陸失敗