Struts2 学习笔记 —— 04 —— Path
2014-01-13 14:34
369 查看
首先配置web.xml
然后配置struts.xml
此时访问项目根路径,由于web.xml中的设置会先去找struts.xml中对应的路径,
不过struts.xml中只有对/path路径的处理,没有对根目录/的处理
所以Struts又交给tomcat处理,tomcat根据web.xml中<wellcome-file-list>显示index.jsp页面
所以此时显示如下
看一下index.jsp的代码
我们看到里面超级链接的地址是path/path.action,说明访问的是项目path的命名空间下的path的action
回头看看struts.xml中的配置,对于path/path.action的处理交由PathAction这个类处理,并且只处理返回值为“path”的结果
下面是PathAction的代码
基于此,我们点击连接后,就能访问指定的path.jsp了
再看一下path.jsp的源码
我们看到这里也有一个超级链接,链接到index.jsp
按说index.jsp与path.jsp同在一个目录下,点击应该能够跳转
但是实际上点击后没有跳转到index.jsp,而是访问到了/path/index.jsp
原因是在path.jsp这个JSP文件中,不会去看这个文件真正的路径在哪里,只看这个JSP映射到目前服务器的链接地址
所以只会根据地址来链接,而不会根据文件真正的路径链接
解决方法很简单:全用绝对路径链接!
但是如果直接改成下面这种绝对路径是不行的
因为它会链接到
因为“/”代表的是整个站点根路径,而不是web application的根路径
MyEclipse会自动生成JSP中的这部分代码
这个basePath就是整个web application的根路径
在使用时,加上这部分basePath(<%= basePath%>)就可以生成完整的绝对路径了
这样就可以成功跳转了
basePath也可以用<% String context = request.getContextPath(); %>
注意,basePath最后包含了“/”
还有一种MyEclipse的方式
在<head>里加入一个<base>标签,表示当前所有页面的链接,默认前面都加上basePath
总结:
struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。
虽然可以用redirect方式解决,但redirect方式并非必要。
解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
或者使用myeclipse经常用的,指定basePath
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
然后配置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="path" namespace="/path" extends="struts-default"> <action name="path" class="com.demo.struts2.path.PathAction"> <result name="path"> /path.jsp </result> </action> </package> </struts>
此时访问项目根路径,由于web.xml中的设置会先去找struts.xml中对应的路径,
不过struts.xml中只有对/path路径的处理,没有对根目录/的处理
所以Struts又交给tomcat处理,tomcat根据web.xml中<wellcome-file-list>显示index.jsp页面
所以此时显示如下
看一下index.jsp的代码
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Index.jsp</title> </head> <body> This is index page!<br><br> <a href="path/path.action">路径问题说明</a> </body> </html>
我们看到里面超级链接的地址是path/path.action,说明访问的是项目path的命名空间下的path的action
回头看看struts.xml中的配置,对于path/path.action的处理交由PathAction这个类处理,并且只处理返回值为“path”的结果
下面是PathAction的代码
public class PathAction extends ActionSupport { public String execute(){ return "path"; } }
基于此,我们点击连接后,就能访问指定的path.jsp了
再看一下path.jsp的源码
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <html> <head> <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>
我们看到这里也有一个超级链接,链接到index.jsp
按说index.jsp与path.jsp同在一个目录下,点击应该能够跳转
但是实际上点击后没有跳转到index.jsp,而是访问到了/path/index.jsp
原因是在path.jsp这个JSP文件中,不会去看这个文件真正的路径在哪里,只看这个JSP映射到目前服务器的链接地址
所以只会根据地址来链接,而不会根据文件真正的路径链接
解决方法很简单:全用绝对路径链接!
但是如果直接改成下面这种绝对路径是不行的
<a href="/index.jsp">index.jsp</a>
因为它会链接到
因为“/”代表的是整个站点根路径,而不是web application的根路径
MyEclipse会自动生成JSP中的这部分代码
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %>
这个basePath就是整个web application的根路径
在使用时,加上这部分basePath(<%= basePath%>)就可以生成完整的绝对路径了
<a href="<%= basePath%>index.jsp">index.jsp</a>
这样就可以成功跳转了
basePath也可以用<% String context = request.getContextPath(); %>
注意,basePath最后包含了“/”
还有一种MyEclipse的方式
在<head>里加入一个<base>标签,表示当前所有页面的链接,默认前面都加上basePath
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %>
<html>
<head>
<base href="<%= basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<a href="index.jsp">index.jsp</a>
</body>
</html>
总结:
struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。
虽然可以用redirect方式解决,但redirect方式并非必要。
解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
或者使用myeclipse经常用的,指定basePath
相关文章推荐
- Struts2学习笔记04 之 拦截器
- Struts2学习笔记-04
- Struts2_学习笔记(二)--------Namespace、Action、Path、ActionMethod_DMI、ActionWildcard
- Struts2_学习笔记(二)--------Namespace、Action、Path、
- Struts2学习笔记04----ActionContext、ServletActionContext、ValueStack之间的关系
- struts2学习笔记2 -struts2的开发步骤和工作原理
- struts2学习之基础笔记8
- iOS学习笔记-078.核心动画04——CATransition(转场动画)
- cocos2d-x学习笔记04:简单动画
- [struts2学习笔记] 第四节 学着使用struts 2的tag标签
- Struts2 学习笔记18 拦截器原理分析
- Struts2学习笔记1
- Struts2拦截器Interceptor学习笔记
- Struts2知识点学习笔记
- Struts2学习笔记14:Struts2的文件上传和下载【续】
- Struts2学习笔记15:Struts2的文件上传和下载【续】二
- Struts2学习笔记----阿冬专栏
- NLP 学习笔记 04 (Machine Translation)
- cocos2dx 3.3 + lua 学习笔记(04)--- 动作回调、函数回调、骨骼动画回调
- Replica Island 学习笔记 04 - AndouKun