SpringMVC 的基本配置

在 web.xml 写入:

<?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">

    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

在 resources 下创建 spring-mvc.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/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 告诉mvc框架,controller包中的类可能会处理请求 -->
    <context:component-scan base-package="com.mine.controller" ></context:component-scan>
    <!-- 开启 mvc 注解 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 处理静态资源访问 -->
    <mvc:default-servlet-handler />
</beans>

创建 com.mine.controller.TestController 类写入:

@Controller
public class OneController {

    @RequestMapping(path = "test")
    @ResponseBody
    public String test(){
        return "hello world!!!";
    }

}

配置tomcat
配置方法见:https://wwp666.blog.csdn.net/article/details/116051200

启动tomcat后访问即可

posted @ 2022-03-07 18:04  叕叕666  阅读(29)  评论(0)    收藏  举报