第一个:
检验用户是否登录,登录了才可以借书和买书,否则提示登录
Action.java
package com.wan;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Action extends ActionSupport {
private static final long serialVersionUID = -2295563990921774523L;
private String username;
private String password;
@Override
public String execute() throws Exception {
ActionContext context=ActionContext.getContext();
if("wh".equals(username)&&"123".equals(password))
{
context.put("username", username);
context.put("password", password);
context.getSession().put("username", username);
context.getSession().put("password", password);
return SUCCESS;
}else
{
context.getApplication().put("error", "登录失败");
return ERROR;
}
}
public void buy()
{
System.out.println("BUY BOOKS!");
}
public void read()
{
System.out.println("READ BOOKS!");
}
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;
}
}
BookAction.java
package com.wan;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BookAction extends ActionSupport {
private static final long serialVersionUID = -2295563990921774523L;
public void buy()
{
System.out.println("BUY BOOKS!");
}
public void read()
{
System.out.println("READ BOOKS!");
}
}
MyInterceptor.java
package com.wan;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class MyInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation arg0) throws Exception {
ActionContext context=arg0.getInvocationContext();
Object obj= context.getSession().get("username");
if(obj!=null){
return arg0.invoke();
}
else{
context.put("message", "请您先登录");
return Action.LOGIN;
}
}
}
struts.xml
<?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>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="myInterceptor" class="com.wan.MyInterceptor"></interceptor>
<interceptor-stack name="allInterceptor">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="login" class="com.wan.Action">
<result name="success">/success.jsp</result>
<result name="error">/fail.jsp</result>
</action>
<action name="book_*" class="com.wan.BookAction" method="{1}">
<interceptor-ref name="allInterceptor"></interceptor-ref>
<result name="success">/success.jsp</result>
<result name="login">/index.jsp</result>
</action>
</package>
</struts>
book.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 'book.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>
<a href="/Struts/book_read">Read Books</a><br/>
<a href="/Struts/book_buy">Buy Books</a><br/>
</body>
</html>
fail.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 'fail.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>
登录失败!
</body>
</html>
index.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 'index.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>
${message }
<form action="login" method="post">
name:<input type="text" name="username"><br/>
password:<input type="password" name="password"><br/>
<input type="submit" name="登录">
</form>
</body>
</html>
success.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 'success.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>
<a href="/Struts/book_read">Read Books</a><br/>
<a href="/Struts/book_buy">Buy Books</a><br/>
登陆成功${requestScope.username } ${password }
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>FirstFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>FirstFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<interceptors>配置所有的拦截器,每个拦截器栈都有默认的拦截器。拦截器引用在action中。
MyInterceptor类判定是否用户登录。
拦截器类里面,若是要继续向下执行,返回 invocation.invoke() ,表示通过拦截,invacation 即为拦截器类里 intercept 函数的参数。
第二个:
这里自定义一个将特殊内容转化为指定内容的实例。
这里将"java"字符串转化为"play"。
这里列出核心代码
struts.xml 定义及配置拦截器
<interceptors>
<interceptor name="interceptor" class="com.wanhao.WordsInterceptor"/>
<interceptor-stack name="loginstack">
<!-- 这个是系统拦截器栈,不这样写,就是当你单独一个action引用拦截器的时候就无法使用拦截器的中的核心功能 -->
<interceptor-ref name="defaultStack"/>
<!-- 这个引用你自己写的拦截器 -->
<interceptor-ref name="interceptor" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loginstack"></default-interceptor-ref>
index.jsp
<form action="test.action" method="post">
内容:<input type="text" name="words"/>
<input type="submit" value="提交">
</form>
Test.java
public class Test extends ActionSupport implements ModelDriven{
private String words;
public String getWords() {
return words;
}
public void setWords(String words) {
this.words = words;
}
@Override
public String execute(){
return "success";
}
@Override
public Object getModel() {
return words;
}
}
show.jsp
${words }
WordsInterceptor.java
public class WordsInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Object bo=invocation.getAction();
if(bo!=null)
{
if(bo instanceof Test)
{
Test t=(Test)bo;
t.setWords(t.getWords().replaceAll("java", "game"));
System.out.println(t.getWords());
return invocation.invoke();
}
}
return "test";
}
}