学习Struts2_0400_path
2015-11-25 22:50
513 查看
path路径问题
当我们直接访问根目录的时候。
我们会先从filter中找有没有对应页面 如果没有就会调用welcome-file 所包含的页面。
这里面的welcome-file 包含的index.jsp页面。
在这里jsp页面中href 连接的是path/path.action
我们这就要看一看stryts.xml怎么配置的。
我们先寻找namespace中的值 是/path class调用了一个类。
该类返回的是path 然后就看struts.xml 中result中的name 是path 然后跳转到该页面、
我们观察一下
这段代码里面request.getScheme()获取的是http
request.getServerName()获取的是本机IP地址
request.getServerPort() 获取的是端口号。
request.getContextPath()获取的是项目名称
这句话加载jsp页面中 路径就是他所配置的路径。
欢迎各位大神 建议和修改。
当我们直接访问根目录的时候。
<?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页面中 路径就是他所配置的路径。
欢迎各位大神 建议和修改。
相关文章推荐
- SpringMVC_springmvc框架
- 数组基本操作(四)
- 数组的基本操作(三)
- Spring、SpringMVC、Mybatis整合遇到的问题
- java8 Lambda 表达式 学习
- 在struts2中延长session的生命周期
- Struts2 在xml文件中敲尖括号”<”没有提示【7】
- java面向对象基础知识(三)
- Java作业——权限综合实验 + 数组、字符串高级应用
- eclipse用android真机调试程序出现的问题
- Java中获取完整的url
- Java作业——学籍管理系统
- Google地图百度地图GPS经纬度偏移转换(JAVA)
- 10020---Java WEB实现URL重写的优缺点及如何实现
- J2EE项目中使用Spring 4及.properties文件UTF-8编码问题
- jdk与jre的区别
- java动态代理
- 停止Java线程,小心interrupt()方法
- Dubbo与Zookeeper、Spring整合和使用
- leetcode_java_adddigits