1.导入必须的jar包
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>StrutsTest</display-name>
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.配置struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.devMode" value="false" />
<include file="com/struts/config/login.xml"></include>
<include file="com/struts/config/login2.xml"></include>
<include file="com/struts/config/user.xml"></include>
<include file="com/struts/config/personnelDepart.xml"></include>
</struts>
最好是在struts里面导入其他路径下的.xml文件。struts.xml里面写include
4.其他.xml 例如user.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="student" extends="struts-default">
<action name="userLogin" class="com.user.servlet.UserLoginAction" method="execute">
<result name="success" type="redirectAction">lookPersonnelDepart</result>
<result name="default" >/default.jsp</result>
<result name="input" >/default.jsp</result>
</action>
<action name="userRegist" class="com.user.servlet.UserRegistAction" method="execute">
<result name="success" >/registSuccess.jsp</result>
<result name="default" >/default.jsp</result>
</action>
<action name="userDelete" class="com.user.servlet.UserDeleteAction" method="execute">
<result name="success" >/deleteSuccess.jsp</result>
<result name="default" >/default.jsp</result>
</action>
<action name="userUpdate" class="com.user.servlet.UserUpdateAction" method="execute">
<result name="success" >/updateSuccess.jsp</result>
<result name="default" >/default.jsp</result>
</action>
</package>
</struts>
5.写一个action类 例子:UserLoginAction
package com.user.servlet;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.user.dao.UserDao;
import com.user.pojo.User;
/**
* 用户登录
* @author 郑平
*/
public class UserLoginAction extends ActionSupport {
private User user =null;
private String id;
private String password;
public void validateExecute() {
// TODO Auto-generated method stub
if(this.id==null || "".equals(this.id.trim())){
System.out.println("有错");
this.addFieldError("id", "id不能为空");
}
if(this.password==null||"".equals(this.password.trim())){
System.out.println("密码不能为空");
this.addFieldError("password", "密码不能为空");
}
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
String values = "success";
System.out.println("id:" + id + "------------------password:" + password);
UserDao userDao = new UserDao();
ActionContext actionContext = ActionContext.getContext();
actionContext.getApplication().put("app", "application内容");
actionContext.put("req", "request内容");
user = userDao.selectUser(id);
actionContext.getSession().put("session", user);
if(!user.getPassword().equals(password)){
System.out.println("密码不相同");
return "default";
}
System.out.println("user.id:" + user.getId()+" user.getPassword():"+ user.getPassword());
return values;
}
public String selectUser() {
UserDao userDao = new UserDao();
@SuppressWarnings("unused")
User user = userDao.selectUser(id);
return "success";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}