分享

Struts2学习笔记----Path路径问题

 集微笔记 2013-07-30

struts中的路径用的是action路径,而不是Jsp路径,所以用的时候尽量不要用相对路径,而是用绝对路径

 

1、index.jsp文件

Java代码 复制代码 收藏代码
  1. <body>   
  2.     <a href="path/path.action">路径问题说明</a>   
  3. </body>  

 

2、path.jsp文件

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="GB18030" ?>   
  2. <%@ page language="java" contentType="text/html; charset=GB18030"  
  3.     pageEncoding="GB18030"%>   
  4.     <%@taglib uri="/struts-tags" prefix="s" %>   
  5. <SPAN style="COLOR: #ff0000">    <%   
  6. String path = request.getContextPath();   
  7. String basePath = request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";   
  8. %></SPAN>   
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  10. <html xmlns="http://www./1999/xhtml">   
  11. <head>   
  12. <SPAN style="COLOR: #ff0000"><base href="<%=basePath%>" /></SPAN>   
  13. <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />   
  14. <title>Insert title here</title>   
  15. </head>   
  16. <body>   
  17. struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。<br />   
  18. <a href="index.jsp">index.jsp</a>   
  19. <br />   
  20. 虽然可以用redirect方式解决,但redirect方式并非必要。   
  21. <br />   
  22. 解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)   
  23. <br />   
  24. 或者使用myeclipse经常用的,指定basePath   
  25. </body>   
  26. </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下)

Java代码 复制代码 收藏代码
  1. package org.hualang.strutsaction;   
  2. public class PathAction {   
  3.     public String execute() {   
  4.         return "path";   
  5.     }   
  6. }  

 4、struts.xml文件

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts./dtds/struts-2.0.dtd">   
  5.   
  6. <struts>   
  7.     <constant name="struts.devMode" value="true" />   
  8.     <package name="path" extends="struts-default" namespace="/path">   
  9.         <action name="path" class="org.hualang.strutsaction.PathAction">   
  10.             <result name="path">/path.jsp</result>   
  11.         </action>   
  12.     </package>   
  13. </struts>  

 运行结果:



 当点击路径问题后会跳转到



 当点击index.jsp会跳转到



 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多