因项目需求边学边开发,故特此记录下来,以便日后再次使用
环境分为3个部分
1.web.xml(WEB-INF根目录)
1.添加filter节点
2.添加filter-mapping节点(过滤标签)
3.listener节点(可不加,tomcat启动时直接运行)
4.完整xml
2.struts.xml(src根目录)
1.默认结点
2.json节点(ajax使用)
3.多个struts.xml
3.BaseAction.java类(用于其他action继承)
4.lib(见附件)
环境分为3个部分
1.web.xml(WEB-INF根目录)
1.添加filter节点
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
2.添加filter-mapping节点(过滤标签)
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.listener节点(可不加,tomcat启动时直接运行)
<listener>
<listener-class>Entity.SqlEntity</listener-class>
</listener>
4.完整xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
<listener>
<listener-class>Entity.SqlEntity</listener-class>
</listener>
</web-app>
2.struts.xml(src根目录)
1.默认结点
<package name="struts" namespace="/" extends="struts-default">
<action name="UserCheck" class="Action.UserCheckAction" method="GetUserCheckList">
<result name="success">
UserCheck.jsp
</result>
<result name="input">
login.jsp
</result>
</action>
</package>
2.json节点(ajax使用)
<package name="struts1" namespace="/" extends="json-default">
<action name="SetUser" class="Action.UserCheckAction" method="text">
<result name="success" type="json">
<param name="root">SqlData</param>
</result>
</action>
</package>
3.多个struts.xml
<include file="struts-code.xml"></include>
3.BaseAction.java类(用于其他action继承)
package Action;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport implements RequestAware,
SessionAware, ServletContextAware, ParameterAware, ServletRequestAware,
ServletResponseAware, ApplicationAware {
private static final long serialVersionUID = 5952689219411916553L;
protected HttpServletRequest request;
protected HttpServletResponse response;
protected HttpSession session;
protected ServletContext application;
@SuppressWarnings("unchecked")
protected Map requestMap;
@SuppressWarnings("unchecked")
protected Map sessionMap;
@SuppressWarnings("unchecked")
protected Map applicationMap;
@SuppressWarnings("unchecked")
protected Map parameterMap;
public void setRequest(Map<String, Object> arg0) {
this.requestMap = arg0;
}
public void setSession(Map<String, Object> arg0) {
this.sessionMap = arg0;
}
public void setServletContext(ServletContext arg0) {
this.application = arg0;
}
public void setParameters(Map<String, String[]> arg0) {
this.parameterMap = arg0;
}
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
this.session = request.getSession();
}
public void setServletResponse(HttpServletResponse arg0) {
this.response = arg0;
}
public void setApplication(Map<String, Object> arg0) {
this.applicationMap = arg0;
}
}
4.lib(见附件)