struts2相关知识

struts2自定义拦截器

1.创建拦截器类,继承AbstractInterceptor,重写intercept()方法。

package com.tianxun.user.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * @author Brokie
 * @date 2017年10月23日
 */
public class LoginInterceptor extends AbstractInterceptor{
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		 // 取得请求相关的ActionContext实例  
        ActionContext ctx = invocation.getInvocationContext();  
        Map session = ctx.getSession();  
        String user = (String) session.get("");  
  
        // 如果没有登陆,或者登陆所有的用户名不是user,都返回重新登陆  
  
        if (user != null && user.equals("user")) {
        	//告诉action接着执行下面的方法
            return invocation.invoke();  
        }  
        ctx.put("error", "你还没有登录");  
        return "login"; 
	}
}

2.在struts.xml中声明拦截器:在package下定义拦截器并定义拦截器栈(默认拦截器:defaultStack)。

3.使用拦截器:在action中使用inceptor-ref调用拦截器栈。

<?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>
		<constant name="struts.enable.DynamicMethodInvocation" value="true" />
		<package name="user" extends="struts-default" namespace="/">
			<!-- 定义一个拦截器 -->  
	        <interceptors>  
	            <interceptor name="authority"  
	                class="com.ywjava.interceptot.LoginInterceptor">  
	            </interceptor>  
	            <!-- 拦截器栈 -->  
	            <interceptor-stack name="mydefault"> 
	            	<!-- 默认拦截器栈 --> 
	                <interceptor-ref name="defaultStack" />
	               	<!-- 自定义拦截器栈 -->  
	                <interceptor-ref name="authority" />  
	            </interceptor-stack>  
	        </interceptors>
	        <!--  action方法  -->
			<action name="*" class="userAction" method="{1}">
				<!-- 使用拦截器 -->
				<interceptor-ref name="mydefault"></interceptor-ref>
				<result name="{1}">WEB-INF/jsp/{1}.jsp</result>
			</action>
		</package>
		
	</struts>

拦截器的作用

权限

日志(运行时间,具体的方法调用)

打印日志

action权限划分

struts2返回值跳转类型

<result-types>
        	<!-- 转发到action -->
            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
            <!--  转发向到jsp,默认的 -->
            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
            <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
            <!-- 重定向到jsp -->
            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
            <!-- 重定向到action -->
            <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
            <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
            <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
            <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
        </result-types>

struts2标签

struts2标签与ognl表达式

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
<%@ taglib uri="/struts-tags" prefix="s" %>  
<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="UTF-8">  
        <title>注册界面</title>  
    </head>  
    <body>  
        <s:form action="register" namespace=""> 
        <!-- 输入框 --> 
            <s:textfield name="username" label="用户名" key="Switch"/>         
            <s:password name="password" label="密码"/>  
            <s:radio name="gender" list="#{'0':'男','1':'女'}" label="性别" value="0" />  
            <s:textfield name="age" label="年龄"/>  
            <s:select name="city" list="#{'bj':'北京','sh':'上海','gz':'广州','sz':'深圳'}" label="城市" headerKey="-1" headerValue="---请选择城市---" emptyOption="true"/>  
            <s:checkboxlist name="hibbies" list="#{'code':'写代码','algorithm':'算法','movie':'电影'}" label="爱好"/>  
            <s:checkbox name="married" label="是否已婚" value="true" labelposition="left"/>  
            <s:textarea name="description" label="自我介绍" rows="5" cols="20"/>  
            <s:file name="phone" label="头像"/>  
            <s:submit value="提交"/>  
            <s:reset value="重置"/>  
        </s:form>  
    </body>  
</html>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值