struts2路径配置_Struts 2结果路径配置示例

struts2路径配置

This is the third post in the Struts 2 series. You might want to check out earlier posts too.

这是Struts 2系列的第三篇文章。 您可能还想查看以前的帖子。

  1. Struts Beginners Tutorial and Hello World XML Example

    Struts初学者教程和Hello World XML示例
  2. Struts 2 Example with Annotation

    带注释的Struts 2示例

In both the earlier posts, we used JSP pages as the result of action, in this post we will see some important points to note with the result location with example.

在之前的两篇文章中,我们都将JSP页面用作操作的结果,在这篇文章中,我们将看到一些重要的要点,并在示例中注明了结果的位置。

<package name="user" namespace="/User" extends="struts-default">
	<action name="home">
		<result>results/login.jsp</result>
	</action>
</package>

Above is the snippet from struts.xml file, notice that result element doesn’t start with a forward slash (/). In this case, struts will look for result JSP page at {WEBAPP-ROOT}/{Namespace}/{result} path. So our JSP file should be present at WEBAPP/User/results/ directory.

上面是struts.xml文件中的代码段,请注意result元素不是以正斜杠(/)开头。 在这种情况下,struts将在{WEBAPP-ROOT} / {Namespace} / {result}路径中寻找结果JSP页面。 因此,我们的JSP文件应该位于WEBAPP / User / results /目录中。

But if result element starts with forward slash, then struts look for file in the root folder. So if struts.xml file package element is:

但是,如果result元素以正斜杠开头,则struts将在根文件夹中查找文件。 因此,如果struts.xml文件包元素为:

<package name="user" namespace="/User" extends="struts-default">
	<action name="home">
		<result>/results/login.jsp</result>
	</action>
</package>

Then struts will look for result page at WEBAPP/results/ directory.

然后,struts将在WEBAPP / results /目录中查找结果页面。

Now take a look at below package declaration.

现在看看下面的包声明。

<package name="user" namespace="/User/" extends="struts-default">
	<action name="home">
		<result>/login.jsp</result>
	</action>
</package>

Since the namespace is ending with “/” struts will look for result at WEBAPP/User/login.jsp even though result element starts with forward slash.

由于名称空间以“ /”结尾,因此即使结果元素以正斜杠开头,struts也会在WEBAPP / User / login.jsp中查找结果。

Let’s see how we can configure the result path if we are using annotation based configuration.

让我们看看如果使用基于注释的配置,如何配置结果路径。

@Namespace("/Test")
@Action("test")
@Result(location="results/login.jsp")
public class TestAction extends ActionSupport {

}

In above case, struts will try to locate file at WEB-INF/content/Test/results/login.jsp.

在上述情况下,struts将尝试在WEB-INF / content / Test / results / login.jsp中定位文件。

Usually we don’t put JSP or HTML files inside WEB-INF directory, so we can provide @Result location attribute value starting with forward slash to let struts look for files in the project root directory.

通常我们不将JSP或HTML文件放在WEB-INF目录中,因此我们可以提供以斜杠开头的@Result位置属性值,以使Struts在项目根目录中查找文件。

If we change above @Result annotation to @Result(location="/results/login.jsp"), then struts will look for file at WEBAPP/results/login.jsp

如果我们将@Result注释上方的内容更改为@Result(location="/results/login.jsp") ,则struts将在WEBAPP / results / login.jsp中查找文件

We can also use @ResultPath annotation to provide the result pages path.

我们还可以使用@ResultPath批注提供结果页面路径。

@Namespace("/Test")
@Action("test")
@ResultPath("/jsps")
@Result(location="login.jsp")
public class TestAction extends ActionSupport {

}

Struts will look for login.jsp at WEBAPP/jsps/Test/ directory.

Struts将在WEBAPP / jsps / Test /目录中查找login.jsp。

If you want to change result path from WEB-INF/content directory to some other directory globally, you can do it through struts configuration file.

如果要将结果路径从WEB-INF / content目录全局更改为其他目录,则可以通过struts配置文件来完成。

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"https://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.convention.result.path" value="/"></constant>
</struts>

That’s all for configuring the result pages location in struts 2, we will look into more of struts 2 features in future posts.

这些都是为了配置结果页面在struts 2中的位置,我们将在以后的文章中进一步研究struts 2的功能。

翻译自: https://www.journaldev.com/2160/struts-2-result-path-configuration-with-examples

struts2路径配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值