学习Struts2_0400_path

来源:互联网 发布:万人网络信息有限公司 编辑:程序博客网 时间:2024/06/16 10:21

path路径问题
当我们直接访问根目录的时候。

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

我们会先从filter中找有没有对应页面 如果没有就会调用welcome-file 所包含的页面。
这里面的welcome-file 包含的index.jsp页面。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'index.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body><a href="path/path.action">路径问题说明</a>  </body></html>

在这里jsp页面中href 连接的是path/path.action
我们这就要看一看stryts.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.enable.DynamicMethodInvocation" value="false"         /> <constant name="struts.devMode" value="true" /> <package name="default"         namespace="/" extends="struts-default"> <default-action-ref name="index"         /> <global-results> <result name="error">/WEB-INF/jsp/error.jsp</result>         </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception"         result="error"/> </global-exception-mappings> <action name="index"> <result         type="redirectAction"> <param name="actionName">HelloWorld</param> <param         name="namespace">/example</param> </result> </action> </package> <include         file="example.xml"/> -->        <!-- 该项配置 改成开发模式。我们该配置会立即反馈 不需要重新启动服务器 -->        <constant name="struts.devMode" value="true" />    <package name="default" namespace="/path" extends="struts-default">        <action name="path" class="com.struts2.path.path">            <result name="path">                /path.jsp            </result>        </action>    </package></struts>

我们先寻找namespace中的值 是/path class调用了一个类。

package com.struts2.path;import com.opensymphony.xwork2.ActionSupport;public class path extends ActionSupport{    @Override    public String execute(){        return "path";    }}

该类返回的是path 然后就看struts.xml 中result中的name 是path 然后跳转到该页面、

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'path.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    <a href="index.jsp">index页面</a>  </body></html>

我们观察一下

<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>

这段代码里面request.getScheme()获取的是http
request.getServerName()获取的是本机IP地址
request.getServerPort() 获取的是端口号。
request.getContextPath()获取的是项目名称

<base href="<%=basePath%>">

这句话加载jsp页面中 路径就是他所配置的路径。

欢迎各位大神 建议和修改。

0 0
原创粉丝点击