有关strut2的path的学习

来源:互联网 发布:手机电脑无网络传文件 编辑:程序博客网 时间:2024/05/22 16:05
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"    xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <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.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="com.bjsxt.struts2.path.action.PathAction">            <result name="path">/path.jsp</result>        </action>    </package></struts>

    当输入地址http://localhost:8080/Struts2_0400_Path/ 直接访问根路径,首先它会到web.xml,到对应的Struts2的filter,然后会找对应的namespace(这里namespace是斜杠/),但是在struts.xml里没有对应的namespace,则会返回到web.xml里,交给服务器去处理(没有找到对应的namespace,没有找到对应的action,就交给服务器去处理),服务器看到index.jsp,就访问index.jsp.
在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>Insert title here</title> </head> <body>     <a href="path/path.action">路径问题说明</a> </body> </html>


    当点链接的时候,应该是去struts.xml里找namespace="/path"和action为"path",当点的时候,访问成功。地址是:http://localhost:8080/Struts2_0400_Path/path/path.action
    访问到path.jsp,里面的内容是:
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>


    里面< a href="index.jsp" >index.jsp< /a >,这个链接地址用的是一个相对路径,正常的情况下,path.jsp去访问index.jsp,因为它们是在同一个目录下面,所以去访问的时候,应该是可以访问的,但是很不幸的是,访问不了。点了之后,地址是:http://localhost:8080/Struts2_0400_Path/path/index.jsp 这是为什么啊?是因为在path.jsp里,它不会去看jsp的真正路径在哪里,它只看目前jsp映射的服务器上的url地址,所以链接的时候是根据http://localhost:8080/Struts2_0400_Path/path/这个地址来链,而不会根据文件真正的路径来链。
    上面正确的应该写成< a href="../index.jsp" >index.jsp< /a >
    这是不是很恐怖?如果以后path改变了,就是namespace改变了,这个链接就又变了。 这样写链接太麻烦了。
    虽然问题很复杂,但是结论很简单,结论就是二话不说,直接用绝对路径链接。
    这样子写行不行?< a href="/index.jsp" >index.jsp< /a > 不行~!打包票百分百不行,它链到http://localhost:8080/index.jsp了,在jsp里面,大家都知道斜杠/代表整个站点的根路径,不是代表web application的根路径,你要是用,还得麻烦点,但是就算它再麻烦,它也是死东西,
    MyEclipse建jsp的时候,会自动帮你生成下面的代码:
 <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
request.getContextPath()会拿到Struts2_0400_Path这个路径,然后用request.getScheme()拿到http这个字符串,request.getServerName()拿到localhost,request.getServerPort()拿到8080,最后就形成了整个http://localhost:8080/Struts2_0400_Path/这部分的内容,我们所有的链接只要都把这部分加上,就成了绝对路径了。所以以后写路径的时候就可以写成这个样子了< a href="<%=basePath% >index.jsp">index.jsp< /a >
    现在再试,就行了。
    另一种方法是在里加上,指定base是什么内容,指定当前所有页面里面的链接前面默认加上basePath.

    虽然过程很复杂,但是结论很简单。
    struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。
    虽然可以用redirect方式解决,但redirect方式并非必要。
    解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)。或者使用myeclipse经常用的,指定basePath
原创粉丝点击