zoukankan      html  css  js  c++  java
  • Structs复习 Path问题

    Path问题相对复杂 主要是路劲问题 但结论很简单 就是统一使用绝对路径

    jar包

    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>

    Structs.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>

    PathAction.java

    package com.bjsxt.struts2.path.action;
    
    public class PathAction {
        public String execute() {
            return "path";
        }
    }

    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>

    index.jso

    <?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>

    输入

    http://localhost:6666/Struts2_0400_Path/index.jsp

  • 相关阅读:
    angular学习笔记(二十五)-$http(3)-转换请求和响应格式
    angular学习笔记(二十四)-$http(2)-设置http请求头
    angular学习笔记(二十三)-$http(1)-api
    Replace 在动态sql中的实现
    SQL 中 replace 替换字符串中的字符 ''
    sqlserver 时间字符串转化为时间格式
    update set where exists 多表更新 在mssql、MySQL、Oracle中分别用法
    LPAD在Oracle中和 mssql以及在MySQL中的不同用法 以及调用存储过程方法
    支付宝对账单下载Java正式商户调用
    支付宝对账单下载Java沙箱调用
  • 原文地址:https://www.cnblogs.com/frankzone/p/9465173.html
Copyright © 2011-2022 走看看