SpringMVC:绝对地址、相对地址(动力)

 

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

<!--    声明,注册springmvc的核心对象DispatcherServlet-->
    <servlet>
        <servlet-name>myweb</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!--        自定义springmvc读取的配置文件的位置-->
        <init-param>
            <!--springmvc的配置文件属性-->
            <param-name>contextConfigLocation</param-name>
            <!-- 指定自定义文件的位置-->
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>

<!--        在tomcat启动后,创建Servlet对象
               load-on-startup:表示tomcat启动后创建对象的顺序,它的值是正数,数值越小 tomacat创建对象越早
-->
        <load-on-startup>1</load-on-startup>

    </servlet>

<!--    所有的*..do请求交给myweb中央处理器处理-->
    <servlet-mapping>
        <servlet-name>myweb</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

 

springmvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!-- 用了注解需要注解扫描器:声明组件扫描器:包扫描-->
    <context:component-scan base-package="com.bjpowernode.controller"/>

<!--    声明springmvc框架中的视图解析器,帮助开发人员设置视图文件的路径-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀:视图文件的路径-->
        <property name="prefix" value="/WEB-INF/view/"/>
        <!-- 后缀:视图文件的扩展名-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

 MyController:

package com.bjpowernode.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/*
* @RequestMapping:
*   value:所有请求地址的公共部分,叫做模块名称
*   位置:放在类的上面
*    通过加上一个类的访问路径,就可以看出这些功能属于user模块的*/
@Controller
public class MyController {
    @RequestMapping(value ="/user/some.do")   //value里面可以写多个名字进行访问
    public ModelAndView doSome(){ //doSome相当于doGet()---调用Service请求处理
        System.out.println("调用Sevice后:");//这里先不调用,简化处理
        ModelAndView mv=new ModelAndView();

        //添加数据,框架在请求的最后把数据放入到request作用域。
        //request.setAttribute("msg","欢迎使用mvc开发")
        mv.addObject("mgs","欢迎使用mvc开发");
        mv.addObject("fun","执行的是dosome方法");

        mv.setViewName("show");

        //返回mv
        return mv;

    }
}

show.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 19:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <base href="<%=basePath%>">
</head>
<body>
 <h3>show.jsp,从request作用域中获取数据</h3>
  <h3>msg数据:${mgs}</h3>
  <h3>fun数据:${fun}</h3>
</body>
</html>

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 17:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <base href="<%=basePath%>">
</head>
<body>
  <h1>hellow word</h1>
  <p><a href="user/some.do">发起some.do的get请求</a></p>

</body>
</html>

 

当访问地址不加 / 时:

访问的是:

当发起user/some.do请求后,地址变为:当前页面的地址加上连接的地址

http://localhost:8888/ch06_path_war_exploded/user/some.do 

 

 

当访问地址加上 / 时:

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 17:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <base href="<%=basePath%>">
</head>
<body>
  <h1>hellow word</h1>
  <p><a href="/user/some.do">发起some.do的get请求</a></p>

</body>
</html>

 

 

地址变为:这个地址缺的是项目名字

http://localhost:8888/user/some.do

参考地址是服务器地址:

可以在访问地址上加上项目名字,就可以正常访问了

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 17:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <base href="<%=basePath%>">
</head>
<body>
  <h1>hellow word</h1>
  <p><a href="/ch06_path_war_exploded/user/some.do">发起some.do的get请求</a></p>

</body>
</html>

http://localhost:8888/user/

 

 通过写上项目名字,但是这样不够灵活,这样写死了,如果项目访问地址(项目名字)改变了:导致url访问地址也得改变

 可以通过EL表达式解决:

代表项目的访问路径:

${pageContext.request.contextPath}

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 17:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <base href="<%=basePath%>">
</head>
<body>
  <h1>hellow word</h1>
  <p><a href="${pageContext.request.contextPath}/user/some.do">发起some.do的get请求</a></p>

</body>
</html>

 base标签

 

 

 改变控制器:MyController:让访问控制器跳转页面还是index.jsp:

package com.bjpowernode.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/*
* @RequestMapping:
*   value:所有请求地址的公共部分,叫做模块名称
*   位置:放在类的上面
*    通过加上一个类的访问路径,就可以看出这些功能属于user模块的*/
@Controller
public class MyController {
    @RequestMapping(value ="/user/some.do")   //value里面可以写多个名字进行访问
    public ModelAndView doSome(){ //doSome相当于doGet()---调用Service请求处理
        System.out.println("调用Sevice后:");//这里先不调用,简化处理
        ModelAndView mv=new ModelAndView();

        //添加数据,框架在请求的最后把数据放入到request作用域。
        //request.setAttribute("msg","欢迎使用mvc开发")
        mv.addObject("mgs","欢迎使用mvc开发");
        mv.addObject("fun","执行的是dosome方法");

        mv.setViewName("/index.jsp");

        //返回mv
        return mv;

    }
}

注释视图解析器:不用视图解析器

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- 前缀:视图文件的路径-->
    <property name="prefix" value="/WEB-INF/view/"/>
    <!-- 后缀:视图文件的扩展名-->
    <property name="suffix" value=".jsp"/>
</bean>

 

index.jsp:假设里面没有,url不加 / 

<base href="<%=basePath%>">
<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 17:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
    
</head>
<body>
  <h1>hellow word</h1>
  <p><a href="user/some.do">发起some.do的get请求</a></p>

</body>
</html>

第一次点击:

 

第二次点击:

 

 

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 17:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <base href="http://localhost:8888/ch06_path_war_exploded/">
</head>
<body>
  <h1>hellow word</h1>
  <p><a href="user/some.do">发起some.do的get请求</a></p>

</body>
</html>

 

 

 

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 17:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <base href="<%=basePath%>">
</head>
<body>
  <h1>hellow word</h1>
  <p><a href="user/some.do">发起some.do的get请求</a></p>

</body>
</html>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
request.getScheme() 获取协议
request.getServerName() 获取服务器IP
request.getServerPort() 获取端口号
getContextPath() 获取返回路径

在index页面中,所有没有 / 开头的参考地址都以<%=basePath%>为参考地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值