struts2中的路径问题

1.先建立一个项目

2.在此我们需要建立3个jsp

1) 在第一个jsp中写入一句话

<body>
    This is my JSP page. <br>
  </body>

2)在第二个jsp中写一个连接

<body>
	<a href="path/path.action">路径问题说明</a>
</body>

3)在第三个jsp钟写入关于struts2中的路径问题

<body>
struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。<br />
<a href="index.jsp">index.jsp</a>
<br />
虽然可以用redirect方式解决,但redirect方式并非必要。
<br />
解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
<br />
或者使用myeclipse经常用的,指定basePath
</body>

3.在java中写一个方法

public class PathAction {
	public String execute() {
		return "path";
	}

返回值为path

4.在struts.xml中写入

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="path" extends="struts-default" namespace="/path">
        <action name="path" class="com.bjsxt.struts2.path.action.PathAction">
            <result name="path">/path.jsp</result>
        </action>
    </package>
</struts>

当你点击链接后便可以跳转到第三个jsp界面

5.当然在web.xml中不可忘记这句哦

 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
原文地址:https://www.cnblogs.com/yumofei/p/5357191.html