注解简化SpringMVC环境搭建

首先配置注解扫描(该包扫描必须放在springmvc配置文件中,如果放在spring配置文件中,则扫描不到)

    <context:component-scan base-package="com.test.controller"/>
<?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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.test"/>
    <!--注解驱动,相当于配置HandlerMapping及HandlerAdapter-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--放行静态资源-->
    <!--一个*表示该文件夹下的所有子文件,**表示该文件夹的所有子文件及子文件夹下的内容-->
    <!--location表示文件资源所在路径,mapping表示请求路径-->
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/"/>
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/"/>
    <mvc:resources mapping="/images/**" location="/WEB-INF/images/"/>
</beans>

controller类:

package com.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DemoController {
    // String表示跳转到哪个页面
    @RequestMapping("demo")
    public String demo() {
        return "main.jsp";
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 对于java中的思考的方向,1必须要看前端的页面,对于前端的页面基本的逻辑,如果能理解最好,不理解也要知道几点。 ...
    神尤鲁道夫阅读 842评论 0 0
  • 1、@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherS...
    jkian阅读 1,012评论 5 14
  • spring官方文档:http://docs.spring.io/spring/docs/current/spri...
    牛马风情阅读 1,732评论 0 3
  • 1、@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherS...
    圣光会制裁你丨阅读 1,680评论 0 23
  • 周日一早醒来,腰酸背痛。刷了邮箱,NYTIMES BREAKING NEWS,飞机事故,确认两人死亡。暗骂一句,从...
    乐喜阅读 155评论 0 1