如何使用Struts2更改默认JSP /模板位置 [英] How to change default JSP/template location with Struts2
问题描述
我正在开发一个在Eclipse中使用Struts2的新Java EE应用程序。我想保留JSP文件在源文件夹( src / main / jsp
),而不是在 WebContent
。部署时,所有源文件将复制到 WebContent / WEB-INF / classes
。这也有使jsp文件不可访问的直接(我想要一切需要操作干预的奖金效果)。这意味着要显示结果,我必须这样做:
I'm working on a new Java EE application that uses Struts2 in Eclipse. I want to keep the JSP files in the source folder (src/main/jsp
) and not in WebContent
. When deployed, the all source files get copied over to WebContent/WEB-INF/classes
. This also has the bonus effect of making the jsp files inaccessible directly (I want everything to require action intervention). This means that to make results display, I have to do this:
<result name="SUCCESS">WEB-INF/classes/index.jsp</result>
可以设置jsp文件的默认位置,使 index.jsp
足以引用它们?理想情况下,文件也会位于 WEB-INF / jsp
中,而不会与类混合。
Is it possible to set the default location of the jsp files so that just index.jsp
is sufficient to reference them? Ideally the files would also sit in WEB-INF/jsp
and not get mixed with the classes.
推荐答案
您可以创建一个常量配置参数,例如
You can create a constant configuration parameter, e.g.
<constant name="struts.result.path" value="/WEB-INF/classes" />
然后将此常量注入自定义 dispatcher
result 。将它添加到默认包中:
Then inject this constant into custom dispatcher
result. Add this to your default package:
<result-types>
<result-type name="dispatcher" default="true" class="struts.results.MyServletDispatcherResult" />
</result-types>
实现很简单,只需在配置结果的位置添加一个前缀。 / p>
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, e.g.
<result name="SUCCESS">/index.jsp</result>
这篇关于如何使用Struts2更改默认JSP /模板位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!