zoukankan      html  css  js  c++  java
  • Struts2中 Path (getContextPath与basePath)

    struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。

    虽然可以用redirect方式解决,但redirect方式并非必要。
    解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextpath方式来拿到webapp的路径)
    或者使用myeclipse经常用的,指定basePath

    例子:

    <?xml version="1.0" encoding="GB18030" ?>
    <%@ page language="java" contentType="text/html; charset=GB18030"
        pageEncoding="GB18030"%>
        <%@taglib uri="/struts-tags" prefix="s" %>
        <%
        //这里的path是得到webapp的名字,如果我们的webapp名字是struts_0400_path
        //那么path就是struts_0400_path
        //basePath包含了path内容,他是全路径:        http://localhost:1000/struts2_0400_paht
        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%>" />是指定根路径。
    <base href="<%=basePath%>" />
    
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
    <title>Insert title here</title>
    </head>
    <body>

    说明:在这里我们使用的链接是index.jsp,但是因为在<head>中我们定义了<base href="<%=basePath%>" />
      所以在这里的链接其实是:http://localhost:1000/struts2_0400_path/index.jsp 路径

      这就是<base href="<%=basePath%>" />的好处之所在了。
    <a href="index.jsp">index.jsp</a>

  • 相关阅读:
    HTTP协议
    MySQL建立主-从服务器双机热备配置
    centOS7安装配置mysql5.7.21
    修改docker镜像的默认存储目录
    docker--学习笔记
    MySQL数据库操作
    zabbix注意事项和常用命令
    CnentOS6.5安装zabbix2.2
    如何查询一个进程下面的线程数(进程和线程区别)
    OSPF与ACL实例
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4185806.html
Copyright © 2011-2022 走看看