SpringMVC环境搭建

建立一个web项目,导入相关依赖。

image


建立web.xml(配置DispatchcerServlet)

image

点击查看代码
   <?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">
     
     <!-- 配置DispatchcerServlet
                      目的:当浏览器传入固定的url拦截它交给DispatchcerServlet判断
     
     
      -->
     <servlet>
       <!-- 名字 -->
       <servlet-name>springDispatcherServlet</servlet-name>
       <!-- 使用的是那个class类创建 -->
       <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 -->
            <servlet-name>springDispatcherServlet</servlet-name>
            <!-- 对所有请求都做拦截类似javaweb的servlet -->
            <url-pattern>/</url-pattern>
   </servlet-mapping>
   
      
   </web-app>

建立SpringMVC.xml

image

点击查看代码
   <?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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-4.0.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
   
   
           <!-- 配置自动扫描的包:只能是对应的包不能是* -->
           <context:component-scan base-package="Controller"></context:component-scan>
   
           <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
               <property name = "prefix" value="/WEB-INF/jsp/"></property>
               <property name = "suffix" value = ".jsp"></property>
           </bean>
   </beans>

建立Controller

image

点击查看代码
   package Controller;
   
   import org.springframework.stereotype.Controller;
   import org.springframework.web.bind.annotation.RequestMapping;
   
   @Controller
   public class TestController {
   
   	 @RequestMapping("/helloworld")
   	 public String hello(){
   		          System.out.println("hello world");
   		          return "success";
   		      }
   }
   

建立jsp文件

image

点击查看代码
   <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
   <!DOCTYPE html>
   <html>
   <head>
   <meta charset="ISO-8859-1">
   <title>Insert title here</title>
   </head>
   <body>
   <a href="helloworld">hello world</a>
   </body>
   </html>
posted @ 2021-10-28 01:36  旅祸少年  阅读(28)  评论(0编辑  收藏  举报