- 默认
- 1.标准URL映射
- 2.Ant风格的映射(通配符)
- 3.占位符的映射(restful风格)
项目结构
TestController3.java
package com.deng.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestController3 {
/**
* 1.标准URL映射
* @RequestMapping(value=”xxx”)
* */
@RequestMapping(value = "hello")
public ModelAndView test() {
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "原来是小别扇1");
mv.setViewName("/hello");
return mv;
}
@RequestMapping("/hello1")
public ModelAndView test1() {
ModelAndView mv = new ModelAndView();
mv.addObject("msg1", "你很会打吗?");
mv.setViewName("/hello1");
return mv;
}
/**
* 2.Ant风格的映射(通配符)
* ?:通配一个字符
* *:配合其他一个字符以上的时候,通配0个或者多个字符,单独使用的时候只能配置多余0个的字符
* **:通配0个或者多个路径, 配合其他一个字符以上的时候通配0或多个字符,而单独使用的时候,可以配置多个路径
* */
@RequestMapping("a?/show2")
public ModelAndView test2() {
ModelAndView mv = new ModelAndView();
mv.addObject("msg2", "springmvc ant style");
mv.setViewName("/hello2");
return mv;
}
/**
* 3.占位符的映射(restful风格)
* @RequestMapping(value=“/user/{userId}/{name} ")
* */
@RequestMapping(value = "/ok/{name}/{id}")
public ModelAndView test3(@PathVariable("name") String name, @PathVariable("id") Long id) {
ModelAndView mv = new ModelAndView();
//接收名字
mv.addObject("msg3", "占位符name: " + name + ",id = " + id);
//jsp名字
mv.setViewName("/hello3");
return mv;
}
}
mv.setViewName(“/hello2”);
hello2是jsp的名字
hello1.jsp
<%--
Created by IntelliJ IDEA.
User: U100926
Date: 2022/08/27
Time: 18:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--少了这一行,jsp页面接收不到值显示${msg}--%>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>springmvc2</title>
</head>
<body>
<span style="font-size:30px; color:darkgreen;">springmvc的第一个程序:${msg1}</span>
<p>${msg1}</p>
</body>
</html>
hello2.jsp
<%--
Created by IntelliJ IDEA.
User: U100926
Date: 2022/08/27
Time: 18:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--少了这一行,jsp页面接收不到值显示${msg}--%>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>springmvc2</title>
</head>
<body>
<span style="font-size:30px; color:darkgreen;">springmvc的第一个程序:${msg2}</span>
<p>${msg3}</p>
</body>
</html>
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">
<!-- 扫描组件,将加上@Controller注解的类作为SpringMVC的控制层 -->
<context:component-scan base-package="com.deng.controller"></context:component-scan>
<!--
配置视图解析器
作用:将prefix + 视图名称 + suffix 确定最终要跳转的页面
-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<!--servlet: DispatcherServlet-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
<!-- <url-pattern>*.do</url-pattern>-->
</servlet-mapping>
</web-app>
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>org.example</groupId>
<artifactId>springmvc2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springmvc2 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</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>4.0.1</version>
</dependency>
<!--springmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springmvc2</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>