[英]What naming convention should be used for custom Interceptor class and package in order to avoid struts.xml in Struts2
I am interested to know if there is any convention for naming custom interceptor in struts2 for the auto detection of interceptors like there are for the action classes. 我有兴趣知道是否有任何约定在struts2中命名自定义拦截器 ,以便自动检测拦截器,就像动作类一样。
I am using " struts2-convention-plugin-2.3.24.1.jar ". 我正在使用“ struts2-convention-plugin-2.3.24.1.jar ”。
The Package structure is as follow 包结构如下
ProtienTracker
>Java Resources
>src
>com.nagarro.actions
>HelloAction.java
>com.nagarro.interceptors
>custInterceptor.java
>WebContent
>META_INF
>WEB_INF
>content
>hello.jsp
>lib
>web.xml
The code runs perfect without " struts.xml " and without " custInterceptor ". 代码在没有“ struts.xml ”且没有“ custInterceptor ”的情况下运行完美。 The action is automatically detected by the struts2-convention-plugin.
struts2-convention-plugin自动检测到该操作。
As soon as i attach the interceptor with 只要我附上拦截器
@org.apache.struts2.convention.annotation.Action(value="hello",
interceptorRefs=@InterceptorRef("custInterceptor"))
i get the error as shown below. 我收到错误,如下所示。
The files are as follow 文件如下
hello.jsp 为hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Hello</title>
</head>
<body>
<h1><s:property value="greeting"/></h1>
<p>hi</p>
</body>
</html>
HelloAction.java HelloAction.java
package com.nagarro.actions;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
public class HelloAction implements Action {
private String greeting="ab";
@Override
@org.apache.struts2.convention.annotation.Action(value="hello",
interceptorRefs=@InterceptorRef("custInterceptor"))
public String execute() throws Exception {
setGreeting("Hello Structs 2");
return "success";
}
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
}
custInterceptor.java custInterceptor.java
package com.nagarro.interceptors;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class custInterceptor implements Interceptor{
private static final long serialVersionUID = 1L;
@Override
public void destroy() {
System.out.println("custInterceptor destroy() is called...");
}
@Override
public void init() {
System.out.println("custInterceptor init() is called...");
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("custInterceptor intercept() is called...");
System.out.println(invocation.getAction().getClass().getName());
return invocation.invoke();
}
}
I get the following error: 我收到以下错误:
Caused by: Unable to find interceptor class referenced by ref-name custInterceptor - [unknown location]
at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:63)
at org.apache.struts2.convention.DefaultInterceptorMapBuilder.buildInterceptorList(DefaultInterceptorMapBuilder.java:95)
at org.apache.struts2.convention.DefaultInterceptorMapBuilder.build(DefaultInterceptorMapBuilder.java:86)
at org.apache.struts2.convention.DefaultInterceptorMapBuilder.build(DefaultInterceptorMapBuilder.java:70)
at org.apache.struts2.convention.PackageBasedActionConfigBuilder.createActionConfig(PackageBasedActionConfigBuilder.java:947)
at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildConfiguration(PackageBasedActionConfigBuilder.java:734)
at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildActionConfigs(PackageBasedActionConfigBuilder.java:355)
at org.apache.struts2.convention.ClasspathPackageProvider.loadPackages(ClasspathPackageProvider.java:53)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:274)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67)
... 17 more
As mentioned in define interceptors with struts2 annotations . 如定义带有struts2注释的拦截器中所述。
you cannot eliminate a need of struts.xml if you are using custom interceptors.
如果您使用自定义拦截器,则无法消除对struts.xml的需求。
To define a new interceptor in xml you need to define it in struts.xml files 要在xml中定义新的拦截器,您需要在struts.xml文件中定义它
<struts>
......
<package name="my-default" extends="struts-default" />
<!-- Define the intercepor class and give it a name-->
<interceptors>
<interceptor name="custInterceptor" class=com.nagarro.interceptors.custInterceptor" />
</interceptors>
<!-- Add it to a package. for example I add
the interceptor at top of struts default stack-->
<interceptor-stack name="myDefaultStack">
<interceptor-ref name="custInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
<!-- Use the interceptor stack in your action with @InterceptorRef or set it as default -->
<default-interceptor-ref name="myDefaultStack" />
</package>
</struts>
You can see struts-default.xml https://struts.apache.org/docs/struts-defaultxml.html as a good sample for how interceptors are defined and used. 您可以看到struts-default.xml https://struts.apache.org/docs/struts-defaultxml.html作为拦截器定义和使用方式的一个很好的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.