struts中的路径用的是action路径,而不是Jsp路径,所以用的时候尽量不要用相对路径,而是用绝对路径
1、index.jsp文件
<body>
<a href="path/path.action">路径问题说明</a>
</body>
2、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>Insert title here</title>
</head>
<body>
struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。<br />
<a href="index.jsp">index.jsp</a>
<br />
虽然可以用redirect方式解决,但redirect方式并非必要。
<br />
解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
<br />
或者使用myeclipse经常用的,指定basePath
</body>
</html>
注意,这里使用base来制定文件的绝对路径
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
getContextPath()根据当前的内容得到路径,getScheme()得到的是http,getServerName()得到的是127.0.0.1,即IP地址,getServerPort()得到当前端口号8080,path得到当前项目所在的路径,也就是Struts2_0400_Path
最后bastPath其实是http://127.0.0.1:8080/Struts2_0400_Path/
3、PathAction.java文件(在包package org.hualang.strutsaction下)
package org.hualang.strutsaction;
public class PathAction {
public String execute() {
return "path";
}
}
4、struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="path" extends="struts-default" namespace="/path">
<action name="path" class="org.hualang.strutsaction.PathAction">
<result name="path">/path.jsp</result>
</action>
</package>
</struts>
运行结果:
当点击路径问题后会跳转到
当点击index.jsp会跳转到
- 大小: 8.1 KB
- 大小: 13.6 KB
分享到:
相关推荐
struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...
struts2-spring-plugin-2.3.15.2.jar ; struts2-json-plugin-2.3.16.3.jarstruts2-spring-plugin-2.3.15.2.jar ; struts2-json-plugin-2.3.16.3.jar
赠送jar包:struts2-json-plugin-2.3.24.jar; 赠送原API文档:struts2-json-plugin-2.3.24-javadoc.jar; 赠送源代码:struts2-json-plugin-2.3.24-sources.jar; 赠送Maven依赖信息文件:struts2-json-plugin-...
struts2-dojo-plugin-2.2.1.jar, 直接导入到Struts2项目里即可,注意版本。
struts2-json-plugin,Struts JSON插件
struts2-ssl-plugin-1.2.1.jar
struts2-convention-plugin-2.3.32
struts2-dojo-plugin-2.3.4 jar 日期控件的jar包 需要的自行下载
struts2-convention-plugin-2.3.15.1.jar
struts2-core-2.2.3.1-sources.jar struts2-core-2.2.3.1-sources.jar struts2-core-2.2.3.1-sources.jar
struts2-convention-plugin-2.3.24.1
默认的struts2-config-browser-plugin包中的ftl文件include标签路径用的相对路径,会找到包内的include文件,将包内ftl里include的路径改成的/开头的全路径。
struts2-dojo-plugin-2.3.15.3.jar.zip
struts2-core-2.5.18.jar包下载,支持struts2的类库下载
struts2-jquery-plugin-3.1.0.jar
struts2-core-2.5.10.jar ,struts核心包,struts2-core-2.5.10.jar
struts2-jfreechart-plugin-2.1.8.1.jar
struts2-convention-plugin-2.1.6.jar
struts2-spring-plugin-2.3.31.jar
struts2的jar之一struts2-core-2.0.14.jar