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后访问即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC的配置主要包括以下几个方面: 1. 配置DispatcherServlet:在web.xml文件中配置DispatcherServlet,作为前端控制器。可以指定DispatcherServlet的映射路径和加载Spring MVC配置文件的位置。 2. 配置Spring MVC的上下文:创建一个Spring的配置文件(通常命名为[servlet名]-servlet.xml),在配置文件中进行相关的配置,如扫描包、视图解析器、处理器映射等。 3. 配置视图解析器:在Spring MVC配置文件中配置视图解析器,用于将逻辑视图名称解析为具体的视图对象。可以使用InternalResourceViewResolver来解析JSP视图,或者使用其他自定义的视图解析器。 4. 配置处理器映射:在Spring MVC配置文件中配置处理器映射,将请求映射到具体的处理器类。可以使用DefaultAnnotationHandlerMapping或RequestMappingHandlerMapping进行注解驱动的处理器映射。 5. 配置处理器适配器:在Spring MVC配置文件中配置处理器适配器,将请求分派给具体的处理器进行处理。可以使用AnnotationMethodHandlerAdapter或RequestMappingHandlerAdapter进行注解驱动的处理器适配。 6. 配置视图解析器:在Spring MVC配置文件中配置视图解析器,用于将ModelAndView对象解析为具体的视图展示给用户。可以使用InternalResourceViewResolver来解析JSP视图,或者使用其他自定义的视图解析器。 以上是Spring MVC的基本配置,根据实际需求还可以配置拦截器、异常处理等。希望对你有所帮助!如果你还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值