struts2_4

02-拦截器准备练习-登陆功能_

 

03-拦截器api介绍_

 

拦截器创建的第一种方式:

package cn.itcast.a_interceptor;

 

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

//拦截器:第一种创建方式

//拦截器生命周期:随项目的启动而创建,随项目关闭而销毁

public class MyInterceptor implements Interceptor {

@Override

//初始化方法

public void init() {

}

@Override

//拦截方法

public String intercept(ActionInvocationarg0) throws Exception {

return null;

}

@Override

//销毁方法

public void destroy() {

}

}

 

 

拦截器第二种创建方式

 

package cn.itcast.a_interceptor;

 

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

 

//创建方式2:继承AbstractInterceptor -> struts2的体贴

//帮我们空实现了initdestory方法.我们如果不需要实现这两个方法,就可以只实现intercept方法

public class MyInterceptor2 extends AbstractInterceptor {

 

@Override

public String intercept(ActionInvocation arg0) throws Exception {

return null;

}

 

}

 

 

拦截器第三种创建方式

 

package cn.itcast.a_interceptor;

 

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

 

//继承:MethodFilterInterceptor方法过滤拦截器

//功能: 定制拦截器拦截的方法.

// 定制哪些方法需要拦截.

// 定制哪些方法不需要拦截

public class MyInterceptor3 extends MethodFilterInterceptor{

 

@Override

protected String doIntercept(ActionInvocation invocation) throws Exception {

//前处理

System.out.println("MyInterceptor3 的前处理!");

//放行

String result = invocation.invoke();

//后处理

System.out.println("MyInterceptor3 的后处理!");

return result;

}

 

}

以上三种创建拦截器

 

拦截器API

 

//放行

String result = invocation.invoke();

 

 

//前处理

System.out.println("MyInterceptor3 的前处理!");

//放行

String result = invocation.invoke();

//后处理

System.out.println("MyInterceptor3 的后处理!");

 

 

return "new String()" ;

 

 

04-拦截器配置介绍_

 

下面我们给Demo1Action_*配置拦截器

 

<package name="inter" namespace="/" extends="struts-default" >

<interceptors>

<!-- 1.注册拦截器 -->

<interceptor name="myInter3" class="cn.itcast.a_interceptor.MyInterceptor3"></interceptor>

<!-- 2.注册拦截器栈 -->

<interceptor-stack name="myStack">

<!-- 自定义拦截器引入(建议放在20个拦截器之前) -->

<interceptor-ref name="myInter3">

<!-- 指定哪些方法不拦截

 <param name="excludeMethods">add,delete</param> -->

 <!-- 指定哪些方法需要拦截 -->

 <param name="includeMethods">add,delete</param>

</interceptor-ref>

<!-- 引用默认的拦截器栈(20个) -->

<interceptor-ref name="defaultStack"></interceptor-ref>

</interceptor-stack>

</interceptors>

<!-- 3.指定包中的默认拦截器栈 -->

<default-interceptor-ref name="myStack"></default-interceptor-ref>

<action name="Demo1Action_*" class="cn.itcast.a_interceptor.Demo1Action" method="{1}" >

<!-- 为Action单独指定走哪个拦截器(栈)

<interceptor-ref name="myStack"></interceptor-ref>-->

<result name="success" type="dispatcher" >/index.jsp</result>

</action>

</package>

 

Demo1Action

 

package cn.itcast.a_interceptor;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class Demo1Action extends ActionSupport {

 

public String add() throws Exception {

System.out.println("Demo1Action_add!");

return SUCCESS;

}

public String delete()throws Exception {

System.out.println("Demo1Action_delete!");

return SUCCESS;

}

public String update()throws Exception {

System.out.println("Demo1Action_update!");

return SUCCESS;

}

public String find()throws Exception {

System.out.println("Demo1Action_find!");

return SUCCESS;

}

}

 

 

package cn.itcast.a_interceptor;

 

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

 

//继承:MethodFilterInterceptor方法过滤拦截器

//功能: 定制拦截器拦截的方法.

// 定制哪些方法需要拦截.

// 定制哪些方法不需要拦截

public class MyInterceptor3 extends MethodFilterInterceptor{

 

@Override

protected String doIntercept(ActionInvocation invocation) throws Exception {

//前处理

System.out.println("MyInterceptor3 的前处理!");

//放行

String result = invocation.invoke();

//后处理

System.out.println("MyInterceptor3 的后处理!");

return result;

}

 

}

 

 

http://localhost:8080/struts2_day04/Demo1Action_add

 

console窗口

 

MyInterceptor3 的前处理!

Demo1Action_add!

八月 13, 2017 10:04:47 下午 org.apache.jasper.compiler.TldLocationsCache tldScanJar

信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.

MyInterceptor3 的后处理!

 

 

05-拦截器拦截方法指定_

 

<interceptor-ref name="myInter3">

<!-- 指定哪些方法不拦截

 <param name="excludeMethods">add,delete</param> -->

 <!-- 指定哪些方法需要拦截 -->

 <param name="includeMethods">add,delete</param>

</interceptor-ref>

 

 

http://localhost:8080/struts2_day04/Demo1Action_add

 

MyInterceptor3 的前处理!

Demo1Action_add!

MyInterceptor3 的后处理!

 

http://localhost:8080/struts2_day04/Demo1Action_delete

 

MyInterceptor3 的前处理!

Demo1Action_delete!

MyInterceptor3 的后处理!

 

 

http://localhost:8080/struts2_day04/Demo1Action_find

 

Demo1Action_find!

 

http://localhost:8080/struts2_day04/Demo1Action_update

 

Demo1Action_update!

 

 

 

06-拦截器练习-登陆校验拦截器_(练习)

 

需求:出了login方法不需要拦截其与方法均需要拦截,在struts2_crm项目里面的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>

<!-- 指定struts2是否以开发模式运行

1.热加载主配置.(不需要重启即可生效)

2.提供更多错误信息输出,方便开发时的调试

 -->

<constant name="struts.devMode" value="true"></constant>

<package name="crm" namespace="/" extends="struts-default" >

<interceptors>

<!-- 注册拦截器 -->

<interceptor name="loginInterceptor" class="cn.itheima.web.interceptor.LoginInterceptor"></interceptor>

<!-- 注册拦截器栈 -->

<interceptor-stack name="myStack">

<interceptor-ref name="loginInterceptor">

<param name="excludeMethods">login</param>

</interceptor-ref>

<interceptor-ref name="defaultStack"></interceptor-ref>

</interceptor-stack>

</interceptors>

<!-- 指定包中的默认拦截器栈 -->

<default-interceptor-ref name="myStack"></default-interceptor-ref>

<!-- 定义全局结果集 -->

<global-results>

<result name="toLogin" type="redirect" >/login.jsp</result>

</global-results>

<global-exception-mappings>

<!-- 如果出现java.lang.RuntimeException异常,就将跳转到名为error的结果 -->

<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>

</global-exception-mappings>

<action name="CustomerAction_*" class="cn.itheima.web.action.CustomerAction" method="{1}" >

<result name="list" >/jsp/customer/list.jsp</result>

<result name="toList" type="redirectAction">

             <param name="actionName">CustomerAction_list</param>

             <param name="namespace">/</param>

         </result>

</action>

<action name="UserAction_*" class="cn.itheima.web.action.UserAction" method="{1}" >

<result name="toHome" type="redirect" >/index.htm</result>

<result name="error"  >/login.jsp</result>

</action>

</package>

</struts>

 

07-struts2标签-通用标签_(了解)

 


 

 

 

 

 

 

 

 

 

 

 

 

package cn.itcast.b_tag;

 

import java.util.ArrayList;

import java.util.List;

 

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

 

public class Demo2Action extends ActionSupport {

 

public String execute() throws Exception {

List<String> list = new ArrayList<>();

list.add("tom");

list.add("jerry");

list.add("jack");

list.add("rose");

list.add("hqy");

ActionContext.getContext().put("list", list);

return SUCCESS;

}

 

}

 

<package name="tag" namespace="/" extends="struts-default" >

<action name="Demo2Action" class="cn.itcast.b_tag.Demo2Action" method="execute" >

<result name="success" type="dispatcher" >/tag1.jsp</result>

</action>

<action name="Demo3Action" class="cn.itcast.b_tag.Demo3Action" method="execute" >

<result name="success" type="dispatcher" >/tag2.jsp</result>

</action>

</package>

<!-- 遍历标签 iterator -->

<!-- ------------------------------------- -->

遍历标签 iterator:<br>

<s:iterator value="#list" >

<s:property /><br>

</s:iterator>

<!-- ------------------------------------- --><hr>

<s:iterator value="#list" var="name" >

<s:property value="#name" /><br>

</s:iterator>

<!-- ------------------------------------- --><hr>

iterator begin="1" end="100" step="1" :<br>

<s:iterator begin="1" end="100" step="1"  >

<s:property />|

</s:iterator>

<!-- ------------------if else elseif------------------- --><hr>

 

<s:if test="#list.size()==4">

list长度为4!

</s:if>

<s:elseif test="#list.size()==3">

list长度为3!

</s:elseif>

<s:else>

list34!

</s:else>

 

<!-- ------------------property 专门用来配合ognl表达式页面取值

可以用来取代el表达式

 ------------------- --><hr>

 

<s:property value="#list.size()" />

<s:property value="#session.user.name" />

tag1.jsp

遍历标签 iterator:
tom
jerry
jack
rose
hqy

 

tom
jerry
jack
rose
hqy

 

iterator begin="1" end="100" step="1" :
1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| 13| 14| 15| 16| 17| 18| 19| 20| 21| 22| 23| 24| 25| 26| 27| 28| 29| 30| 31| 32| 33| 34| 35| 36| 37| 38| 39| 40| 41| 42| 43| 44| 45| 46| 47| 48| 49| 50| 51| 52| 53| 54| 55| 56| 57| 58| 59| 60| 61| 62| 63| 64| 65| 66| 67| 68| 69| 70| 71| 72| 73| 74| 75| 76| 77| 78| 79| 80| 81| 82| 83| 84| 85| 86| 87| 88| 89| 90| 91| 92| 93| 94| 95| 96| 97| 98| 99| 100|

 

list不3不4!

 

5

 

08-struts2标签-表单标签_

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

   <%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<!-- struts2表单标签 -->

<!-- 好处1: 内置了一套样式.  -->

<!-- 好处2: 自动回显,根据栈中的属性  -->

<!-- theme:指定表单的主题

xhtml:默认

simple:没有主题

 -->

<s:form action="Demo3Action" namespace="/" theme="xhtml" >

<s:textfield name="name" label="用户名"  ></s:textfield>

<s:password name="password" label="密码" ></s:password>

<s:radio list="{'男','女'}" name="gender" label="性别" ></s:radio>

<s:radio list="#{1:'男',0:'女'}" name="gender" label="性别" ></s:radio>

<s:checkboxlist list="#{2:'抽烟',1:'喝酒',0:'烫头'}" name="habits" label="爱好" ></s:checkboxlist>

<s:select list="#{2:'大专',1:'本科',0:'硕士'}" headerKey="" headerValue="---请选择---" name="edu" label="学历" >

</s:select>

<s:file name="photo" label="近照" ></s:file>

<s:textarea name="desc" label="个人简介" ></s:textarea>

<s:submit value="提交" ></s:submit>

</s:form>

<s:actionerror/>

</body>

</html>


源代码:

 

 

   

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script>"undefined"==typeof CODE_LIVE&&(!function(e){var t={nonSecure:"8123",secure:"8124"},c={nonSecure:"http://",secure:"https://"},r={nonSecure:"127.0.0.1",secure:"gapdebug.local.genuitec.com"},n="https:"===window.location.protocol?"secure":"nonSecure";script=e.createElement("script"),script.type="text/javascript",script.async=!0,script.src=c[n]+r[n]+":"+t[n]+"/codelive-assets/bundle.js",e.getElementsByTagName("head")[0].appendChild(script)}(document),CODE_LIVE=!0);</script></head>

<body data-genuitec-lp-enabled="false" data-genuitec-file-id="wc4-2" data-genuitec-path="/struts2_day04/WebContent/tag2.jsp">

<!-- struts2表单标签 -->

<!-- 好处1:内置了一套样式.  -->

<!-- 好处2:自动回显,根据栈中的属性  -->

<!-- theme:指定表单的主题

xhtml:默认

simple:没有主题

 -->

<form id="Demo3Action" name="Demo3Action" action="/struts2_day04/Demo3Action.action" method="post">

<table class="wwFormTable">

<tr>

    <td class="tdLabel"><label for="Demo3Action_name" class="label">用户名:</label></td>

    <td

><input type="text" name="name" value="zhangtingting" id="Demo3Action_name"/></td>

</tr>

 

 

<tr>

    <td class="tdLabel"><label for="Demo3Action_password" class="label">密码:</label></td>

    <td

><input type="password" name="password" id="Demo3Action_password"/></td>

</tr>

 

 

<tr>

    <td class="tdLabel"><label for="Demo3Action_gender" class="label">性别:</label></td>

    <td

><input type="radio" name="gender" id="Demo3Action_gender" value=""/><label for="Demo3Action_gender"></label>

<input type="radio" name="gender" id="Demo3Action_gender" value=""/><label for="Demo3Action_gender"></label>

<input type="radio" name="gender" id="Demo3Action_gender不男不女" value="不男不女"/><label for="Demo3Action_gender不男不女">不男不女</label>

</td>

</tr>

 

 

 

<tr>

    <td class="tdLabel"><label for="Demo3Action_gender" class="label">性别:</label></td>

    <td

><input type="radio" name="gender" id="Demo3Action_gender1" value="1"/><label for="Demo3Action_gender1"></label>

<input type="radio" name="gender" id="Demo3Action_gender0" value="0"/><label for="Demo3Action_gender0"></label>

</td>

</tr>

 

 

 

<tr>

    <td class="tdLabel"><label for="Demo3Action_habits" class="label">爱好:</label></td>

    <td

><input type="checkbox" name="habits" value="2"       id="Demo3Action_habits-1"        />

<label        for="Demo3Action_habits-1"        class="checkboxLabel">抽烟</label>

<input type="checkbox" name="habits" value="1"       id="Demo3Action_habits-2"        />

<label        for="Demo3Action_habits-2"        class="checkboxLabel">喝酒</label>

<input type="checkbox" name="habits" value="0"       id="Demo3Action_habits-3"        />

<label        for="Demo3Action_habits-3"        class="checkboxLabel">烫头</label>

<input type="hidden" id="__multiselect_Demo3Action_habits" name="__multiselect_habits"

       value="" /></td>

</tr>

 

 

 

<tr>

    <td class="tdLabel"><label for="Demo3Action_edu" class="label">学历:</label></td>

    <td

><select name="edu" id="Demo3Action_edu">

    <option value=""

    >---请选择---</option>

    <option value="2">大专</option>

    <option value="1">本科</option>

    <option value="0">硕士</option>

 

 

</select>

 

</td>

</tr>

 

 

<tr>

    <td class="tdLabel"><label for="Demo3Action_photo" class="label">近照:</label></td>

    <td

><input type="file" name="photo" value="" id="Demo3Action_photo"/></td>

</tr>

 

 

<tr>

    <td class="tdLabel"><label for="Demo3Action_desc" class="label">个人简介:</label></td>

    <td

><textarea name="desc" cols="" rows="" id="Demo3Action_desc"></textarea></td>

</tr>

 

 

<tr>

    <td colspan="2"><div align="right"><input type="submit" id="Demo3Action_0" value="提交"/>

</div></td>

</tr>

 

 

</table></form>

 

 

 

 

<ul class="errorMessage">

            <li><span>郝强勇!你错了!!!!</span></li> </ul>

 

</body>

</html>

theme=”simple”

 

 

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

 

<html>

 

<head>

 

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 

<title>Insert title here</title>

 

<script>"undefined"==typeof CODE_LIVE&&(!function(e){var t={nonSecure:"8123",secure:"8124"},c={nonSecure:"http://",secure:"https://"},r={nonSecure:"127.0.0.1",secure:"gapdebug.local.genuitec.com"},n="https:"===window.location.protocol?"secure":"nonSecure";script=e.createElement("script"),script.type="text/javascript",script.async=!0,script.src=c[n]+r[n]+":"+t[n]+"/codelive-assets/bundle.js",e.getElementsByTagName("head")[0].appendChild(script)}(document),CODE_LIVE=!0);</script></head>

 

<body data-genuitec-lp-enabled="false" data-genuitec-file-id="wc4-2" data-genuitec-path="/struts2_day04/WebContent/tag2.jsp">

 

<!-- struts2表单标签 -->

 

<!-- 好处1: 内置了一套样式. -->

 

<!-- 好处2: 自动回显,根据栈中的属性 -->

 

<!-- theme:指定表单的主题

 

xhtml:默认

 

simple:没有主题

 

-->

 

<form id="Demo3Action" name="Demo3Action" action="/struts2_day04/Demo3Action.action" method="post">

 

<input type="text" name="name" value="zhangtingting" id="Demo3Action_name"/>

 

<input type="password" name="password" id="Demo3Action_password"/>

 

<input type="radio" name="gender" id="Demo3Action_gender男" value="男"/><label for="Demo3Action_gender男">男</label>

 

<input type="radio" name="gender" id="Demo3Action_gender女" value="女"/><label for="Demo3Action_gender女">女</label>

 

<input type="radio" name="gender" id="Demo3Action_gender不男不女" value="不男不女"/><label for="Demo3Action_gender不男不女">不男不女</label>

 

 

 

<input type="radio" name="gender" id="Demo3Action_gender1" value="1"/><label for="Demo3Action_gender1">男</label>

 

<input type="radio" name="gender" id="Demo3Action_gender0" value="0"/><label for="Demo3Action_gender0">女</label>

 

 

 

<input type="checkbox" name="habits" value="2" id="Demo3Action_habits-1" />

 

<label for="Demo3Action_habits-1" class="checkboxLabel">抽烟</label>

 

<input type="checkbox" name="habits" value="1" id="Demo3Action_habits-2" />

 

<label for="Demo3Action_habits-2" class="checkboxLabel">喝酒</label>

 

<input type="checkbox" name="habits" value="0" id="Demo3Action_habits-3" />

 

<label for="Demo3Action_habits-3" class="checkboxLabel">烫头</label>

 

<input type="hidden" id="__multiselect_Demo3Action_habits" name="__multiselect_habits"

 

value="" />

 

 

 

<select name="edu" id="Demo3Action_edu">

 

<option value=""

 

>---请选择---</option>

 

<option value="2">大专</option>

 

<option value="1">本科</option>

 

<option value="0">硕士</option>

 

 

 

 

 

</select>

 

 

 

 

 

<input type="file" name="photo" value="" id="Demo3Action_photo"/>

 

<textarea name="desc" cols="" rows="" id="Demo3Action_desc"></textarea>

 

<input type="submit" id="Demo3Action_0" value="提交"/>

 

 

 

</form>

 

 

 

 

 

 

 

 

 

 

 

<ul class="errorMessage">

 

<li><span>郝强勇!你错了!!!!</span></li> </ul>

 

 

 

</body>

 

</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值