我正在开发一个在 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.
我看到 spring 有这个功能.我希望 Struts2 也能做到这一点.
I saw that spring has this feature. I'm hoping for the same thing for Struts2.
可以创建一个常量配置参数,例如
You can create a constant configuration parameter, e.g.
<constant name="struts.result.path" value="/WEB-INF/classes" />
然后将此常量注入到自定义 dispatcher
结果中.将此添加到您的默认包中:
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>
实现很简单,只需在配置结果的位置添加前缀即可.
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/模板位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!