1. 添加依赖文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.bjpowernode</groupId> 8 <artifactId>ssm-ch02</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 <maven.compiler.source>1.8</maven.compiler.source> 15 <maven.compiler.target>1.8</maven.compiler.target> 16 </properties> 17 18 <dependencies> 19 <dependency> 20 <groupId>junit</groupId> 21 <artifactId>junit</artifactId> 22 <version>4.11</version> 23 <scope>test</scope> 24 </dependency> 25 <!--servlet依赖--> 26 <dependency> 27 <groupId>javax.servlet</groupId> 28 <artifactId>javax.servlet-api</artifactId> 29 <version>3.1.0</version> 30 <scope>provided</scope> 31 </dependency> 32 <!--springmvc的依赖--> 33 <dependency> 34 <groupId>org.springframework</groupId> 35 <artifactId>spring-webmvc</artifactId> 36 <version>5.2.5.RELEASE</version> 37 </dependency> 38 </dependencies> 39 40 <build> 41 42 </build> 43 </project>View Code
2. 写web页面
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <title>Title</title> 5 </head> 6 <body> 7 <h3>springmvc的简单使用</h3> <br> 8 <h3><a href="some.do">some.do</a></h3><br> 9 <h3><a href="other.do">other.do</a></h3> 10 <h3><a href="first.do">first.do</a></h3> 11 </body> 12 </html>View Code
3. 写web.xml(实现*处理器功能)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 version="4.0"> 6 <!--添加*处理器--> 7 <servlet> 8 <servlet-name>myDispatcher</servlet-name> 9 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 10 <!--设置读取配置文件路径--> 11 <init-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>classpath:/conf/springmvc.xml</param-value> 14 </init-param> 15 <!--设置启动顺序--> 16 <load-on-startup>1</load-on-startup> 17 </servlet> 18 <servlet-mapping> 19 <servlet-name>myDispatcher</servlet-name> 20 <url-pattern>*.do</url-pattern> 21 </servlet-mapping> 22 </web-app>View Code
4. 写springmvc配置文件(实现组件扫描器,视图解析器)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 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"> 6 <!--设置组件扫描器--> 7 <context:component-scan base-package="com.bjpowernode.controller"></context:component-scan> 8 <!--设置视图解析器--> 9 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 10 <property name="prefix" value="/WEB-INF/view/"></property> 11 <property name="suffix" value=".jsp"></property> 12 </bean> 13 </beans>View Code
5. 写前端逻辑代码,实现对应方法处理逻辑
1 package com.bjpowernode.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.servlet.ModelAndView; 6 7 @Controller 8 public class MyController { 9 @RequestMapping(value = {"/some.do","/other.do"}) 10 public ModelAndView dosome(){ 11 ModelAndView mv = new ModelAndView(); 12 mv.addObject("msg1","我是添加的第一个mv参数"); 13 mv.addObject("msg2","我是添加的第二个mv参数"); 14 mv.setViewName("result"); 15 return mv; 16 } 17 @RequestMapping("/first.do") 18 public ModelAndView dofirst(){ 19 ModelAndView mv = new ModelAndView(); 20 mv.addObject("msg3","我是通过dofirst方法添加的参数"); 21 mv.setViewName("first"); 22 return mv; 23 } 24 }View Code
6. 写结果实现页面 result.jsp,first.jsp……
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <title>返回的结果页面</title> 5 </head> 6 <body> 7 <h3>WEB-INF/view/result.jsp返回结果页面</h3><br> 8 <h3>msg1:${msg1}</h3><br> 9 <h3>msg2:${msg2}</h3> 10 11 </body> 12 </html>View Code
1 <%-- 2 Created by IntelliJ IDEA. 3 User: lihp 4 Date: 2021/6/13 5 Time: 10:33 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>返回first.jsp的结果页面</title> 12 </head> 13 <body> 14 <h3>WEB-INF/view/result.jsp返回结果页面</h3><br> 15 <h3>msg3:${msg3}</h3><br> 16 17 18 </body> 19 </html>View Code