struts2相关知识
来源:互联网 发布:js input不可编辑 编辑:程序博客网 时间:2024/06/05 08:42
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{@Overridepublic 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>
阅读全文
0 0
- Struts2相关知识整合
- struts2相关知识
- struts2相关知识
- Struts2相关知识(一)
- 关于struts2框架配置文件的相关知识
- 细说struts2中一些相关的知识
- struts2-ValueStack相关知识(OGNL)
- struts2的流程和一系列相关知识
- 重新整理struts2与spring整合相关知识
- struts2--(3)--一些小的知识----访问web相关元素
- 电商之梳理struts2相关知识---开发基础框架
- 关于Struts2的相关知识总结及应用
- struts2知识
- struts2知识
- Struts 2.0相关知识 摘自Struts2权威指南(李刚 著)
- 有关struts2的知识
- struts2知识总结
- Struts2 OGNL学习知识
- Multiple annotations found at this line:解决
- 选中gridview其中一行单击删除按钮实现行删除功能
- javaweb中环境变量的配置——详细步骤
- 用SpannableString打造绚丽多彩的文本显示效果
- 5分钟快速生成各类小程序
- struts2相关知识
- 组件、插件、控件的区别
- 布署好自己的tomcat后在网页打不开
- 接口测试【前段与后端】
- opencv-OpenCV中的图像处理 [2]-OpenCV中的轮廓
- [S3-E438]Angular开山篇
- 如何在ListPreference中改变AlertDialog的文字大小?
- WSO2 ESB 5.0.0 文档
- 高频电路设计布线技巧十项规则