[英]How to change default JSP/template location with Struts2
I'm working on a new Java EE application that uses Struts2 in Eclipse. 我正在开发一个在Eclipse中使用Struts2的新Java EE应用程序。 I want to keep the JSP files in the source folder (
src/main/jsp
) and not in WebContent
. 我想将JSP文件保存在源文件夹(
src/main/jsp
)中而不是WebContent
。 When deployed, the all source files get copied over to WebContent/WEB-INF/classes
. 部署后,所有源文件都将复制到
WebContent/WEB-INF/classes
。 This also has the bonus effect of making the jsp files inaccessible directly (I want everything to require action intervention). 这也有使jsp文件直接无法访问的额外效果(我希望一切都需要动作干预)。 This means that to make results display, I have to do this:
这意味着要显示结果,我必须这样做:
<result name="SUCCESS">WEB-INF/classes/index.jsp</result>
Is it possible to set the default location of the jsp files so that just index.jsp
is sufficient to reference them? 是否可以设置jsp文件的默认位置,以便只有
index.jsp
足以引用它们? Ideally the files would also sit in WEB-INF/jsp
and not get mixed with the classes. 理想情况下,文件也会位于
WEB-INF/jsp
而不会与类混合。
I saw that spring has this feature . 我看到春天有这个功能 。 I'm hoping for the same thing for Struts2.
我希望Struts2能做同样的事情。
You can create a constant configuration parameter, eg 您可以创建一个常量配置参数,例如
<constant name="struts.result.path" value="/WEB-INF/classes" />
Then inject this constant into custom dispatcher
result. 然后将此常量注入自定义
dispatcher
结果。 Add this to your default package: 将其添加到您的默认包:
<result-types>
<result-type name="dispatcher" default="true" class="struts.results.MyServletDispatcherResult" />
</result-types>
The implementation is easy, you just add a prefix to the location of the result when it's configured. 实现很简单,只需在配置结果时为结果的位置添加前缀即可。
public class MyServletDispatcherResult extends ServletDispatcherResult {
private String resultPath;
public String getResultPath() {
return resultPath;
}
@Inject(value = "struts.result.path", required = false)
public void setResultPath(String resultPath) {
this.resultPath = resultPath;
}
@Override
public void setLocation(String location) {
super.setLocation(resultPath+location);
}
public MyServletDispatcherResult() {
super();
}
// @Inject
// public MyServletDispatcherResult(String location) {
//
// super(resultPath+location);
// }
}
Then you can use ordinary locations in the results, eg 然后,您可以在结果中使用普通位置,例如
<result name="SUCCESS">/index.jsp</result>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.