首先:配置springMVC最主要的都是它的配置文件,所以从简单的配置文件演示
SpringMVC.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!--开启SpringMVC注解驱动 其可以代替HandleMapping和HandleAdapter 如果去掉注解驱动,那么就无法加载HandleMapping与HandleAdapter那么无论是哪种方式都会是404异常 --> <mvc:annotation-driven></mvc:annotation-driven>
<!--如果不是注解方式实现,那么就需要自己指定自己配置自己的Controller的访问路径-->
<bean name="/*.do" class="com.sxt.controller.NumController"/>
<!-- 加载注解扫描的包。 --> <context:component-scan base-package="com.sxt.controller"/> <!--静态资源的放行 由于web.xml文件中配置路径和tomcat中的路径重叠 所以在这里配置静态资源的放行,让其不用加载到DispatcherServlet mapping:页面请求的时候的路径。 location:表示本地,如果本地没有这个路劲会报错哦 --> <mvc:resources mapping="/js/**" location="/js/"/> </beans>
第二点:springMVC的控制器:
方式一:注解方式:
@Controller public class NumController { @RequestMapping("/add") public ModelAndView getAddNum(Integer num1, Integer num2){ System.out.println(num1+"-----------"+num2); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("c",(num1+num2)); modelAndView.setViewName("index.jsp"); return modelAndView; } }
方式二:实现接口Controller:
package com.sxt.controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @program: * @description: * @author: Mr.Zhang * @create: 2021-09-23 11:12 **/ public class NumController implements Controller { @Override public ModelAndView handleRequest( HttpServletRequest req, HttpServletResponse resp) throws Exception { String num1 = req.getParameter("num1"); String num2 = req.getParameter("num2"); System.out.println(num1+"-----------"+num2); Integer result = Integer.parseInt(num1)+Integer.parseInt(num2); ModelAndView mav = new ModelAndView(); mav.setViewName("index.jsp"); mav.addObject("c",result); return mav; } }
方式三:实现自HHttpRequestHandler
package com.sxt.controller; import org.springframework.web.HttpRequestHandler; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @program: * @description:SpringMVC第三种实现方式 * @author: Mr.Zhang * @create: 2021-09-23 11:12 **/ public class NumController implements HttpRequestHandler { @Override public void handleRequest( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String num1 = req.getParameter("num1"); String num2 = req.getParameter("num2"); System.out.println(num1+"-----------"+num2); Integer result = Integer.parseInt(num1)+Integer.parseInt(num2); req.setAttribute("c",result); req.getRequestDispatcher("index.jsp").forward(req,resp); } }
Web.xml加载SpringMVC容器
<?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中的Controller配置 --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!--给DispatcherServlet的父类FrameworkServlet的属性赋值,让其加载spring配置文件--> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </init-param> <!-- 设置其初始化方法在Tomcat启动的时候自动加载, 如果是0那么就会在第一次发送请求的时候调用初始化方法init(config) --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <!--这么设置和Tomcat里面的配置路劲一样,所以会覆盖掉原有的servlet路径请求 但是这个DispatcherServlet是不会加载静态的js下包的,所以需要在Spring配置文件设置 一个直接放行的路径。 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
jsp前端:
<%-- Created by IntelliJ IDEA. User: 86183 Date: 2021/9/23 Time: 11:08 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form action="add.do" method="post"> num1:<input type="text" name="num1" ><br> num1:<input type="text" name="num2" ><br> <input type="submit" value="提交"> </br> </form> <hr> <div>C:${c}</div> </body> </html>