相对路径与绝对路径个人理解

绝对路径分为本地绝对路径和网路绝对路径两种

本地绝对路径:即物理路径,某一个具体的文件所在的磁盘的路径

网路绝对路径:即对某一资源进行访问时的IP地址

相对路径也分为两种包括以'/'开头的和不以'/'开头的

以/开头的又分为前台相对路径和和后台相对路径

前台相对路径:由浏览器进行解析执行的代码当中所包含的路径。eg:form表单对象当中所包含的Action路径,超链接路径等

后台相对路径:由服务器对象来进行解析执行的代码当中所包含的路径。eg:java代码当中的路径,xml文当中的路径,以及jsp页面当中java代码当中所包含的以/开头的路

径都属于后台相对路径。

在一个web应用当中前后台相对路径之间的差别是:

前台相对路径的相对参考路径是web服务器的根路径,即http://localhost:8080/或http://127.0.0.1:8080/

后台相对路径的相对参考路径则是web服务器的应用路径,即web服务器根路径当中的具体的web项目,http://localhost::8080/project/

不以/开头的相对路径无论是在前头还是在后台,其参考路径为当前资源访问的路径,而非当前资源所在的路径。

具体例子如下:

login.jsp:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>表单登录界面</title>
</head>
<body>
<!--form action="/test/login.action" 有/之后为前台相对路径,该相对路径等价于http:localhost:8080/test/login.action
但是在http:127.0.0.1:8080/web服务器目录当中并没有一个名字为test的项目,当前项目名字为struts2Demo03所以将会在
表单提交之后出现404错误.要想解决404错误就要向服务器对象指明当期所访问的到底时web应用当中的那个项目所以Action改为
form action="Struts2Demo03/test/login.action" 即可正常的进行资源的访问操作-->
<!-- action="test/login.action" 为不带/的相对路径该路径的参照路径为为当前的login.jsp页面所要访问的路径(注意不是login.jsp页面所在的路径,以为在进行多次提交失败
时Action对象当中所要访问的路径是会发生变化的),即http:locathost:8080/Struts2Demo03所以当前的相对路径等价于
http://localhost:8080/Struts2Demo03/test/login.action  该路径指明了在web应用项目当中的具体的哪一个项目,所以不会出现上述404错误
action="${pageContext.request.contextPath }/test/login.action" pageContext.request.contextPath为获取当前页面对象所在的目录路径,即等价于http://localhost:8080/Struts2Demo03/-->
<form action="test/login.action" method="post">
<!--使用当前方式在表单提交失败时,访问的路径每次都是会发生变化的-->
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age">
<input type="submit" value="登录">
</form>
</body>
</html>
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Struts2Demo03</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.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>
struts.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>
<!-- namespace="/test" 为后台相对路径,参照路径为当前项目 该相对路径相当于 http://127.0.0.1:8080/Struts2Demo003/test-->
	<package name="loginPackage" namespace="/test" extends="struts-default">
		<action name="login" class="com.action.LoginAction">
			<result name="success">/welcome.jsp</result>
			<result name="fail">/login.jsp</result>
		</action>
	</package>
</struts>
loginAction.java
package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String execute() throws Exception {
		System.out.println("name="+name);
		System.out.println("age="+age);
		return "fail";
	}
}
welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功界面</title>
</head>
<body>
<h2>使用EL表单式来进行输出操作</h2>
	name=${name}<br>
	age=${age}
</body>
</html>





  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值