一、web.xml
  • 配置tomcat启动时加载DispatcherServlet
  • 配置spring配置文件的路径和名称
<web-app version="4.0"       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">       <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>             <!-- 自启动,tomcat启动时加载,否则第一次访问时加载 -->             <load-on-startup>1</load-on-startup>       </servlet>       <servlet-mapping>             <servlet-name>springmvc</servlet-name>             <url-pattern>/</url-pattern>       </servlet-mapping>   <display-name>Archetype Created Web Application</display-name> </web-app>   二、springmvc.xml
  • 配置HandlerMapping
<?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:aop="http://www.springframework.org/schema/aop"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans         https://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/aop         https://www.springframework.org/schema/aop/spring-aop.xsd         http://www.springframework.org/schema/context          https://www.springframework.org/schema/context/spring-context.xsd">              <bean id="demo123" class="top.woldcn.controller.Controller"></bean>     <bean  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">        <property name="urlMap">              <map>                    <!-- 解析出来的控制器 -->                    <entry key="demo" value-ref="demo123"></entry>              </map>        </property>     </bean>         </beans>   三、controller.java package top.woldcn.controller; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; public class Controller implements  org.springframework.web.servlet.mvc.Controller{       @Override       public ModelAndView handleRequest(HttpServletRequest request,  HttpServletResponse response) throws Exception {             PrintWriter out=response.getWriter();             out.write("hello");             out.flush();             out.close();             return null;       } }  
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄