标签:
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>struts2</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- StrutsPrepareAndExecuteFilter是自2.1.3以后对FilterDispicther的替换 -->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name><!-- 此处的name和<filter>中的name一致 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_2.3.dtd">
<struts>
<!--写相关配置-->
</struts>
package com.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
@Override
public String execute()throws Exception{
System.out.println("执行Action");
return SUCCESS;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_2.3.dtd">
<struts>
<package name="delfaut" namespace="/" extends="struts-default">
<!--
package:类似于java中的包
name:包的名字,可以自定义
namespace:书面值得命名空间,默认为‘/‘,如果未填,当你页面请求action时就会到根目录寻找包名为login下的action,如果你填写了namespace为‘/system‘,就会到相应的目录下寻找,如果没找到,还会到根目录下继续寻找对应的action,如果还是未找到就会报错
extends:类似于java中的继承特征,extends="struts-default"就是继承struts-default.xml,它里面定义了许多跳转类型、拦截器等一些常用的东西
-->
<action name="struts2" class="com.struts2.action.HelloWorldAction">
<result name="">/result.jsp</result>
</action>
</package>
</struts>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘result.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
</body>
</html>
<?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.xml配置包含
1.常量参数配置
2.文件包含导入配置
3.命名空间的配置
4.包的配置
5.Action配置
6.拦截器配置
-->
<struts>
<!--1.常量参数配置
参数主要设置一些struts的环境或者其他属性
可以在Struts2的核心配置文件struts.propertie配置导入
-->
<!-- 设置action的扩展名 -->
<constant name="struts.action.extension" value="action" />
<!-- 设置默认编码 -->
<constant name="struts.i18n.encoding" value="utf8" />
<!-- 设置默认locale -->
<constant name="struts.locale" value="zh_CN" />
<!-- 设置国际化资源文件,多个文件用逗号隔开 -->
<constant name="struts.custom.i18n.resources" value="messageResourse" />
<!-- 设置struts默认ObjectFactory为spring -->
<constant name="struts.objectFactory" value="spring" />
<!-- 设置允许静态方法调用 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<!-- 设置应用是否处于开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- 设置默认主题 -->
<constant name="struts.ui.theme" value="simple" />
<!--2.文件包含导入配置
在struts2中配置文件可分为多个,我可以通过struts.xml来包含其他配置文件
file属性指定文件名
-->
<include file="struts-user.xml"/>
<!--3.命名空间的配置
考虑到struts2中可能会出现同名的Action,所以采用命名空间来区别
同一个命名空间不能出现名字相同的Action,我们通过在包中指定namespace
参数值来指定,如果未指定,默认为"",如果指定命名空间,访问action时候
需要在action name前添加命名空间,例如:http://localhost:8080/工程名/命名空间/Action名.后缀
-->
<package name="default" extends="struts-default" namespace="test">
<action name="action_name" class="com.struts2.action.HelloWorldAction">
<result>/result.jsp</result>
</action>
</package>
<!--4.包的配置
包配置中的属性:(1)name:指定包的名字,必填
(2)extends:指定包继承的包,可以继承其他包的Action定义和拦截器定义,可选
(3)namespace:指定包的命名空间,可选
-->
<package name="default" extends="struts-default" namespace="test">
<!-- 定义全局result -->
<global-results>
<result name="返回值名">页面相对地址</result>
</global-results>
<!-- 定义全局异常映射 -->
<global-exception-mappings>
<exception-mapping result="excption result"
exception="exception class" />
</global-exception-mappings>
<!--5.Action配置
Action中的属性
(1)name:指定action名,用于访问链接
(2)class:指定Action实现类的路径
-->
<action name="sayhello" class="com.struts2.action.HelloAction">
<!-- 设置名为success的result -->
<result>/welcome.jsp</result>
<!--设置名为input的result -->
<result name="input">/index.jsp</result>
<!-- 在action中定义自定义和默认拦截器 -->
<interceptor-ref name="自定义拦截器名"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<!--6.拦截器配置
-->
<!-- 配置自定义拦截器 -->
<interceptors>
<interceptor name="拦截器名" class="拦截器绝对地址"></interceptor>
</interceptors>
</package>
</struts>
package com.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Action默认调用该执行方法
*/
@Override
public String execute()throws Exception{
System.out.println("execute方法执行了!");
return SUCCESS;
}
/**
* add方法
*/
public String add(){
System.out.println("add方法执行了!");
return SUCCESS;
}
/**
* delete方法
*/
public String delete(){
System.out.println("delete方法执行了!");
return SUCCESS;
}
}
<?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>
<!-- 设置action的扩展名 -->
<constant name="struts.action.extension" value="action" />
<package name="default" extends="struts-default" namespace="/">
<!-- 对应默认的excute方法 -->
<action name="sayhello" class="com.struts2.action.HelloWorldAction">
<!-- 设置名为success的result -->
<result name="success">/default.jsp</result>
</action>
<!-- 对应add方法 -->
<action name="addAction" method="add" class="com.struts2.action.HelloWorldAction">
<result name="success">/add.jsp</result>
</action>
<!-- 对应delete方法 -->
<action name="deleteAction" method="delete" class="com.struts2.action.HelloWorldAction">
<result name="success">/delete.jsp</result>
</action>
</package>
</struts>
package com.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Action默认调用该执行方法
*/
@Override
public String execute()throws Exception{
System.out.println("execute方法执行了!");
return SUCCESS;
}
/**
* add方法
*/
public String add(){
System.out.println("add方法执行了!");
return "add";
}
/**
* delete方法
*/
public String delete(){
System.out.println("delete方法执行了!");
return "delete";
}
}
<?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>
<!-- 设置action的扩展名 -->
<constant name="struts.action.extension" value="action" />
<!-- 配置开启动态调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="default" extends="struts-default" namespace="/">
<action name="sayhello" class="com.struts2.action.HelloWorldAction">
<!-- 设置名为success的result -->
<result name="success">/default.jsp</result>
<result name="add">/add.jsp</result>
<result name="delete">/delete.jsp</result>
</action>
</package>
</struts>
<?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>
<!-- 设置action的扩展名 -->
<constant name="struts.action.extension" value="action" />
<package name="default" extends="struts-default" namespace="/">
<action name="*_*" method="{2}" class="com.struts2.action.{1}Action">
<!-- 设置名为success的result -->
<result name="success">/{2}.jsp</result>
<result name="add">/{2}.jsp</result>
<result name="delete">/{2}.jsp</result>
</action>
</package>
</struts>
<form action="login.action" method="post">
用户名:<input type="text" id="username" name="username"><br> 密码:<input
type="password" id="password" name="password"><br> <input type="submit"
value="提交">
</form>
<?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>
<!-- 设置action的扩展名 -->
<constant name="struts.action.extension" value="action" />
</package> -->
<package name="default" extends="struts-default" namespace="/">
<action name="login" method="login" class="com.struts2.action.LoginAction">
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
package com.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
/*
* 属性的get set方法
*/
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 login(){
System.out.println("用户名:"+username);
System.out.println("密码:"+password);
return "success";
}
}
<form action="login.action" method="post">
用户名:<input type="text" id="username" name="user.username"><br> 密码:<input
type="password" id="password" name="user.password"><br> <input type="submit"
value="提交">
</form>
package com.struts2.bean;
public class User {
private String username;
private String password;
/*
* 属性的get set方法
*/
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;
}
}
package com.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
import com.struts2.bean.User;
public class LoginAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
/*
* 登陆方法
*/
public String login() {
System.out.println("用户名:" + user.getUsername());
System.out.println("密码:" + user.getPassword());
return "success";
}
}
package com.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.struts2.bean.User;
public class LoginAction extends ActionSupport implements ModelDriven<User> {
private User user = new User();
/*
* 登陆方法
*/
public String login() {
System.out.println("用户名:" + user.getUsername());
System.out.println("密码:" + user.getPassword());
return "success";
}
/**
* 继承自接口ModelDriven得方法
* @return User
*/
@Override
public User getModel() {
return user;
}
}
<!-- 设置action的扩展名 -->
<constant name="struts.action.extension" value="action" />
//Map<String, Object> application = ActionContext.getContext().getApplication();
Map<String, Object> application1 = (Map<String, Object>) ActionContext.getContext().get("application");
//Map<String, Object> session=ActionContext.getContext().getSession();
Map<String, Object> session1 = (Map<String, Object>) ActionContext.getContext().get("session");
Map<String, Object> request1= (Map<String, Object>) ActionContext.getContext().get("request");
package com.struts2.action;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.struts2.bean.User;
public class LoginAction extends ActionSupport implements ModelDriven<User>,
ServletRequestAware, ServletResponseAware, SessionAware,
ApplicationAware {
private User user = new User();
private HttpServletRequest request;
private Map<String, Object> application;
private HttpServletResponse response;
private Map<String, Object> session;
/*
* 登陆方法
*/
public String login() {
application.put("application", "application data");
request.setAttribute("request", "request data");
session.put("session", "session");
return "success";
}
/**
* 继承自接口ModelDriven得方法
*
* @return User
*/
@Override
public User getModel() {
return user;
}
/**
* 继承自接口ApplicationAware得方法
*/
@Override
public void setApplication(Map<String, Object> arg0) {
application =arg0;
}
/**
* 继承自接口SessionAware得方法
*/
@Override
public void setSession(Map<String, Object> arg0) {
session =arg0;
}
/**
* 继承自接口ServletResponseAware得方法
*/
@Override
public void setServletResponse(HttpServletResponse arg0) {
response =arg0;
}
/**
* 继承自接口ServletRequestAware得方法
*/
@Override
public void setServletRequest(HttpServletRequest arg0) {
request =arg0;
}
}
// 获得HttpServletRequest对象
HttpServletRequest request = ServletActionContext.getRequest();
// 获得HttpServletResponse对象
HttpServletResponse response = ServletActionContext.getResponse();
// 获得HttpSession对象
HttpSession session = ServletActionContext.getRequest().getSession();
// 获得ServletContext对象
ServletContext application = ServletActionContext.getServletContext();
<package name="default" extends="struts-default" namespace="/">
<!-- 注意这里的 name参数,要和下面action的name值保持一致-->
<default-action-ref name="error"></default-action-ref>
<!-- 全局默认action可以执行相应的Action操作,也可以直接放回某个页面 -->
<action name="error" >
<result>/error.jsp</result>
</action>
</package>
在action执行前拦截用户的某些请求并执行某些操作的的组件称为拦截器,多个拦截器就组成了拦截器栈。(类似于web中的过滤器)
1) 数据转移;2)类型转换;3)数据校验;3)用户权限校验
5.3.1 通过实现Interceptor接口实现自定义拦截器
1)搭建好Struts2框架
2)建立登陆页面login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘login.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="login.action" method="post">
用户名:<input type="text" id="username" name="username"><br> 密码:<input
type="password" id="password" name="password"><br> <input type="submit"
value="提交">
</form>
</body>
</html>
3)拦截器MyInterceptor.java继承Intercepter
package com.struts2.Interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
@SuppressWarnings("serial")
public class MyInterceptor implements Interceptor{
@Override
public void destroy() {
}
@Override
public void init() {
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
//执行action之前执行的操作
long startTime = System.currentTimeMillis();
System.out.println("执行action之前执行intercept执行操作");
//执行之后的拦截器,如果没有则执行action操作
String result = invocation.invoke();
//执行action之后执行的操作
long endTime = System.currentTimeMillis();
System.out.println("执行action之后执行intercept执行操作");
System.out.println("执行action花费的时间:"+(endTime-startTime));
return result;
}
}
package com.struts2.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 javax.servlet.jsp.PageContext;
import javax.xml.ws.Response;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.struts2.bean.User;
public class LoginAction extends ActionSupport implements ModelDriven<User> {
private User user = new User();
/*
* 登陆方法
*/
public String login() {
// 获得HttpServletRequest对象
HttpServletRequest request = ServletActionContext.getRequest();
// 获得HttpServletResponse对象
HttpServletResponse response = ServletActionContext.getResponse();
// 获得HttpSession对象
HttpSession session = ServletActionContext.getRequest().getSession();
// 获得ServletContext对象
ServletContext application = ServletActionContext.getServletContext();
application.setAttribute("application", "application data");
request.setAttribute("request", "request data");
session.setAttribute("session", "session");
// 放到request作用域中
ActionContext.getContext().put("value", "附加参数");
// 获取用户名请求参数
System.out.println("用户名:" + user.getUsername());
System.out.println("密码:" + user.getPassword());
return "success";
}
/**
* 继承自接口ModelDriven得方法
*
* @return User
*/
@Override
public User getModel() {
return user;
}
}
<?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>
<!-- 设置action的扩展名 -->
<constant name="struts.action.extension" value="action" />
<package name="default" extends="struts-default" namespace="/">
<!-- 注册拦截器 -->
<interceptors>
<interceptor name="myInterceptor" class="com.struts2.Interceptor.MyInterceptor">
</interceptor>
<!-- 定义拦截器栈,加入默认拦截器 -->
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="myInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="login" method="login" class="com.struts2.action.LoginAction">
<!-- 应用拦截器栈-->
<interceptor-ref name="myStack"></interceptor-ref>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
<interceptor-ref name="params" />
5.3.2 继承AbstractInterceptor类实现自定义拦截器
继承AbstractInterceptor类的实现方法和上一种方法基本相同,唯一不同的是拦截器类MyInterceptor.java的实现,具体区别可见代码
package com.struts2.Interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
@SuppressWarnings("serial")
public class MyInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// 执行action之前执行的操作
long startTime = System.currentTimeMillis();
System.out.println("执行action之前执行intercept执行操作");
//执行之后的拦截器,如果没有则执行action操作
String result = invocation.invoke();
// 执行action之后执行的操作
long endTime = System.currentTimeMillis();
System.out.println("执行action之后执行intercept执行操作");
System.out.println("执行action花费的时间:" + (endTime - startTime));
return result;
}
}
注:内建的拦截器可以在struts2-core-2.3.9.jar包下的struts2-default.xml文件中查看
参看:常见内建拦截器的作用
ps:拦截器的应用需要注意顺序,同时如果没有指定拦截器struts会继承使用默认的拦截器栈,如果用户有自定义的拦截器时,默认拦截器需要显式调用,否则无效。
标签:
原文地址:http://blog.csdn.net/qq_28057577/article/details/51778567