- 浏览: 154712 次
- 性别:
- 来自: 深圳
最新评论
-
决战冰河:
你好,我遇到一个相似的问题。soap请求包为:<para ...
axis 调用cxf boolean获取为null或者异常 -
dingding5060:
额,LZ啥意思?之前都不安装jdk的吗?不知道LZ是否有这样的 ...
org.apache.cxf.interceptor.Fault: Could not send Message -
lxdhq1011:
你好,我想问一下android调用webservice,如何调 ...
在android中调用webservice
查阅了网络上的文章,同时结合自己的实践,将今天的学习结果分享如下,欢迎大家拍砖。
/WEB-INF/lib/下包含以下jar包(但不只这两个)
struts2-core-2.1.6.jar
struts2-convention-plugin-2.1.6.jar
Interceptor代码如下:
view plaincopy to clipboardprint?
package com.kompin.kind.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthorityInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Enter AuthorityInterceptor");
Map session = ActionContext.getContext().getSession();
Object userId = session.get("userId");
if (userId == null)
{
return "login";
}
else
{
return invocation.invoke();
}
}
}
package com.kompin.kind.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthorityInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Enter AuthorityInterceptor");
Map session = ActionContext.getContext().getSession();
Object userId = session.get("userId");
if (userId == null)
{
return "login";
}
else
{
return invocation.invoke();
}
}
}
struts.xml文件配置如下:
view plaincopy to clipboardprint?
<struts>
<constant name="struts.118n.encoding" value="GBK"/>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<package name="kompinInterceptor" extends="struts-default">
<interceptors>
<interceptor name="authority"
class="com.kompin.kind.action.interceptor.AuthorityInterceptor"/>
<interceptor-stack name="auctionStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="authority"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="auctionStack"/>
<global-results>
<result name="login">/WEB-INF/jsp/login.jsp</result>
<result name="exception">/WEB-INF/jsp/error.jsp</result>
</global-results>
</package>
</struts>
<struts>
<constant name="struts.118n.encoding" value="GBK"/>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<package name="kompinInterceptor" extends="struts-default">
<interceptors>
<interceptor name="authority"
class="com.kompin.kind.action.interceptor.AuthorityInterceptor"/>
<interceptor-stack name="auctionStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="authority"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="auctionStack"/>
<global-results>
<result name="login">/WEB-INF/jsp/login.jsp</result>
<result name="exception">/WEB-INF/jsp/error.jsp</result>
</global-results>
</package>
</struts>
User action代码如下:
view plaincopy to clipboardprint?
package com.kompin.kind.action;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.kompin.kind.business.UserBean;
import com.kompin.kind.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("kompinInterceptor")
@InterceptorRefs({
@InterceptorRef("defaultStack")
})
@Results({
@Result(name="login", location="/WEB-INF/jsp/login.jsp")
})
public class UserAction extends ActionSupport {
private Integer id;
private String name;
private String pass;
private String email;
private UserService userSvc;
private List<UserBean> users;
private UserBean user;
@Action(value="/regist",
results={
@Result(name="success", type="dispatcher", location="/WEB-INF/jsp/login.jsp")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/failure.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/regist.jsp")
}
)
public String regist()throws Exception{
try {
if(null == getName() ||null == getPass() || null == getEmail()){
return "input";
}
if (userSvc.addUser(getName(), getPass(), getEmail()) > 0) {
return "success";
}
return "failure";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("注册用户出现异常");
}
}
@Action(value="/proShowUser",
interceptorRefs=@InterceptorRef("authority"),
results={
@Result(name="success", type="dispatcher", location="/WEB-INF/jsp/viewUser.jsp")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/failure.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/login.jsp")
}
)
public String show()throws Exception{
try {
setUsers(userSvc.findAllUser());
return "success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("显示用户出现异常");
}
}
...略...
}
package com.kompin.kind.action;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.kompin.kind.business.UserBean;
import com.kompin.kind.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("kompinInterceptor")
@InterceptorRefs({
@InterceptorRef("defaultStack")
})
@Results({
@Result(name="login", location="/WEB-INF/jsp/login.jsp")
})
public class UserAction extends ActionSupport {
private Integer id;
private String name;
private String pass;
private String email;
private UserService userSvc;
private List<UserBean> users;
private UserBean user;
@Action(value="/regist",
results={
@Result(name="success", type="dispatcher", location="/WEB-INF/jsp/login.jsp")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/failure.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/regist.jsp")
}
)
public String regist()throws Exception{
try {
if(null == getName() ||null == getPass() || null == getEmail()){
return "input";
}
if (userSvc.addUser(getName(), getPass(), getEmail()) > 0) {
return "success";
}
return "failure";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("注册用户出现异常");
}
}
@Action(value="/proShowUser",
interceptorRefs=@InterceptorRef("authority"),
results={
@Result(name="success", type="dispatcher", location="/WEB-INF/jsp/viewUser.jsp")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/failure.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/login.jsp")
}
)
public String show()throws Exception{
try {
setUsers(userSvc.findAllUser());
return "success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("显示用户出现异常");
}
}
...略...
}
Kind action代码如下:
view plaincopy to clipboardprint?
package com.kompin.kind.action;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.kompin.kind.business.KindBean;
import com.kompin.kind.service.KindService;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("kompinInterceptor")
@InterceptorRefs({
@InterceptorRef("auctionStack")
})
@Results({
@Result(name="login", location="/WEB-INF/jsp/login.jsp")
})
public class KindAction extends ActionSupport {
private KindService kindSvc;
private List kinds;
private String kindName;
private String kindDesc;
private KindBean kind;
/**
*
* @param kindSvc
*/
@Action(value="/proAddKind",
results={
@Result(name="success", type="chain", location="proViewKind")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/addKind.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/addKind.jsp")
}
)
public String addKind()throws Exception{
try {
if (kindSvc.addKind(kindName, kindDesc) > 0) {
return "success";
}
return "input";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "input";
}
}
...略...
}
package com.kompin.kind.action;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.kompin.kind.business.KindBean;
import com.kompin.kind.service.KindService;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("kompinInterceptor")
@InterceptorRefs({
@InterceptorRef("auctionStack")
})
@Results({
@Result(name="login", location="/WEB-INF/jsp/login.jsp")
})
public class KindAction extends ActionSupport {
private KindService kindSvc;
private List kinds;
private String kindName;
private String kindDesc;
private KindBean kind;
/**
*
* @param kindSvc
*/
@Action(value="/proAddKind",
results={
@Result(name="success", type="chain", location="proViewKind")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/addKind.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/addKind.jsp")
}
)
public String addKind()throws Exception{
try {
if (kindSvc.addKind(kindName, kindDesc) > 0) {
return "success";
}
return "input";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "input";
}
}
...略...
}
要点说明:
1. @ParentPackage("kompinInterceptor")必须和struts.xml的package的name一致,否则启动tomcat会报错:
"Unable to find interceptor class referenced by ref-name ..."
2. 如果Action中并不是所有的方法都要auctionStack拦截,参考UserAction的代码:
view plaincopy to clipboardprint?
@InterceptorRefs({
@InterceptorRef("defaultStack")
})
@InterceptorRefs({
@InterceptorRef("defaultStack")
})
3. 如果Action中所有的方法都经过同一个拦截器拦截,,参考KindAction的代码:
view plaincopy to clipboardprint?
@InterceptorRefs({
@InterceptorRef("auctionStack")
})
@InterceptorRefs({
@InterceptorRef("auctionStack")
})
4. 如果Action中的某些方法需要特定的拦截器拦截,参考UserAction的代码(请留意proShowUser的Action注解与regist的Action注解的不同点):
view plaincopy to clipboardprint?
@Action(value="/proShowUser",
interceptorRefs=@InterceptorRef("authority"),
results={
@Result(name="success", type="dispatcher", location="/WEB-INF/jsp/viewUser.jsp")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/failure.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/login.jsp")
}
)
@Action(value="/proShowUser",
interceptorRefs=@InterceptorRef("authority"),
results={
@Result(name="success", type="dispatcher", location="/WEB-INF/jsp/viewUser.jsp")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/failure.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/login.jsp")
}
)
5. 留意<interceptor-stack name="auctionStack">的定义。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kompin_dmx/archive/2010/11/06/5992256.aspx
发表评论
-
memcache集群服务:memagent配置使用
2013-05-25 18:29 1002本文来自:http://zhumeng8337797.blog ... -
FastDFS、nginx配置手记
2013-05-23 20:25 836FastDFS、nginx配置手记 (20 ... -
在SQLServer中使用正则表达式
2012-12-21 10:41 2137本文来自: http://www.cnblogs.com/sd ... -
struts convention plugin to scan jar files for actions
2012-08-27 09:40 921本文摘自:http://lerluc.iteye.com/ ... -
CMD命令大全
2011-12-10 20:13 955命令简介 cmd是command的缩写.即命令行 。 ... -
Freemarker+XML导出Word
2011-09-27 17:58 4843本来来自http://blog.csdn.net/x ... -
struts2中显示message
2011-06-16 09:36 1005在struts2.0的action中如何 ... -
使用 Struts2.16 annotation 注解方式配置拦截器的注意事项
2011-06-16 00:11 1380最近在使用Struts2.16 的An ... -
Spring+Hibernate使用时jar包的冲突[转]
2011-05-28 09:03 862在使用Spring的AOP编程时,会用到这几个lib:asm- ... -
Struts2的REST插件[转]
2011-05-28 08:56 3203此REST插件简化了人们和电脑客户端对REST风格资源的访问。 ... -
java23种设计模式简述
2011-05-24 09:59 7681、工厂模式:客户类和 ... -
onbeforeunload事件
2011-05-11 10:16 995<script> function LeaveW ... -
Returning Data from Stored Procedures
2011-04-13 08:56 886该文章来自:http://www.cleardata.biz/ ... -
Sql Server锁表LOCK设置
2011-02-24 12:14 1066Sql Server锁表就限制不同的事物在同一时间内不允许同时 ...
相关推荐
spring2.5 ibatis2.3 struts2.1 dwr3 annotation集成配置
struts2.1 spring2.5 ibatis2.3 dwr3 annotation配置 集成 此中例子不完整. 如要下载:http://download.csdn.net/source/2138885
struts2 annotation 注解的详细介绍及使用方法
3) 利用Spring的@Service、@Repository两个Annotation来实现Service类、DAO类在Spring容器中的自动注册,利用@Autowired来实现Service与其他对象的引用。 通过框架提供的以上组件,除了基本的配置,如数据源、事务等...
用注解(annotation)方式配置struts2 (webwork) 中action的拦截器(interceptor)
struts2 使用Annotation配置实现的小例子,转载供大家学习使用,里面包括了所需的jar包
该代码是Android Annotation 实例代码,便于你理解注解
参考博文:http://blog.csdn.net/u011638419/article/details/41743049
Spring IOC Annotation 注入 学习实例
struts2,json-plugin, 并全注解零配置
通过产生临时文件下载之后,再通过线程删除临时文件
Convention 插件彻底地抛弃了配置信息,不仅不需要使用 struts.xml 文件进行配置,甚至不需要使用 Annotation 进行配置。而是由 Struts 2 根据约定来自动配置。 Convention 这个单词的翻译过来就是“约定”的意思。...
马士兵视频里的struts2 hibernate3 spring2.5 annotation 整合 绝对好用 里面还有jar包 部署就可以用
Struts2的“零配置”特性是Struts2的新功能,可能会出现一些小Bug,所以企业开发者请慎重使用该特性, ...如果用的是Annotation的Struts2,就要将struts.xml去掉,否则即使将struts.xml中的内容注销,也会报错;
Spring基于注释(Annotation)的配置.pdfSpring基于注释(Annotation)的配置.pdfSpring基于注释(Annotation)的配置.pdfSpring基于注释(Annotation)的配置.pdfSpring基于注释(Annotation)的配置.pdfSpring基于...
前段时间很困惑的文件下载问题,现贴出代码,希望能帮到需要的人吧
学习JPA——Hibernate_Annotation使用实例学习JPA——Hibernate_Annotation使用实例学习JPA——Hibernate_Annotation使用实例
struts annotation.ppt 欢迎下载
struts2-Annotation