目录
<%
String basePath = request.getScheme() + "://" + //获取协议
request.getServerName() + ":" + request.getServerPort() + //服务器名字和端口
request.getContextPath() + "/"; //返回站点的跟路劲
%>
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.tjdz</groupId>
<artifactId>ch06-path</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--springmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
</dependencies>
<build>
</build>
</project>
2.index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme() + "://" + //获取协议
request.getServerName() + ":" + request.getServerPort() + //服务器名字和端口
request.getContextPath() + "/"; //返回站点的跟路劲
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<h5>解决jsp页面中的路径问题,用"/"还是不用</h5>
<p><a href="user/some.do">发起some.do请求</a></p>
</body>
</html>
3.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
需要在tomcat服务器启动后,创建DispatcherServlet实例。
为什么要创建DispatcherServlet对象的实例呢?
因为DispatcherServlet在他创建过程中,会同时创建springmvc容器对象,
读取springmvc的配置文件,把这个配置文件中的对象都创建好,当用户发起请求
时就可以直接使用对象了。
servlet的初始化会执行init()方法。DispatcherServlet在init()中{
//创建容器,读取配置文件
WebApplicationContext ctx = new ClassPathXmlApplicationContext("springmvc.xml");
//把容器对象放到ServletContext中
getServletContext().setAttribute(key,ctx);
}
启动tomcat报错,读取这个文件 /WEB-INF/springmvc-servlet.xml(/WEB-INF/myweb-servlet.xml)
springmvc创建容器对象时,读取的配置文件默认是 /WEB-INF/<servlet-name>-servlet.xml
-->
<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启动后创建对象顺序。它的值是整数,数值越小,
tomcat创建的对象时间越早。大于等于0的整数
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myweb</servlet-name>
<!--
使用框架的时候,url-pattern可以使用两种值
1.使用扩展名方式,语法 *.xxxx ,xxxx是自定义文件的扩展名。常用的方式有 *.do,*.action,*.mvc等等
http://locatlhost:8080/myweb/some.do
http://locatlhost:8080/myweb/other.do
2.使用斜杠 "/"
-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
4. springmvc-config.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="edu.tjdz.controller"/>
<!--声明springmvc框架中的视图解析器,帮助开发人员设置视图文件的路径-->
<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!–前缀 : 视图文件的路径–>
<property name="prefix" value="/WEB-INF/view/"/>
<!–后缀 : 视图文件的扩展名–>
<property name="suffix" value=".jsp"/>
</bean>-->
</beans>
5. MyController
package edu.tjdz.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;
@Controller
@RequestMapping
public class MyController {
@RequestMapping(value = "/user/some.do",method=RequestMethod.GET)
public ModelAndView doSome(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","欢迎使用springmvc做web开发");
mv.addObject("fun","执行的是doSome()方法");
mv.setViewName("/index.jsp");
return mv;
}
}