Struts2学习笔记——Path

本文讨论了Struts2中的路径问题及其解决策略,强调了统一使用绝对路径的重要性,并提供了实例说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。
虽然可以用redirect方式解决,但redirect方式并非必要。
解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)

或者使用myeclipse经常用的,指定basePath

Struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
  	<constant name="struts.devMode" value="true" />
  	<package name="front" namespace="/path" extends="struts-default">
       <!-- 配置默认的访问路径 -->
        <action name="path" class="com.smile.struts2.front.action.IndexAction">
            <result name="path"> /path.jsp  </result>
        </action>
    </package>
    
    <package name="main" namespace="" extends="struts-default">
		
       <!--  <default-action-ref name="index" /> -->
       <!-- 配置默认的访问路径 -->
        <action name="index">
            <result>
                /Namespace.jsp
            </result>
        </action>
    </package>
  	
    <!-- Add packages here -->

</struts>
index.jsp:

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>

<%--
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//在head中<base href>指定basePath
--%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Index</title>
</head>
<body>
	<a href="path/path.action">路径问题说明</a>
</body>
</html>
path.jsp:

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
    <%
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--  <base href="<%=basePath%>" />-->
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Path</title>
</head>
<body>
struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。<br />
<a href="<%=basePath %>index.jsp">index.jsp</a>
<br />
虽然可以用redirect方式解决,但redirect方式并非必要。
<br />
解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
<br />
或者使用myeclipse经常用的,指定basePath
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值