码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc的注解开发

时间:2020-01-21 20:16:16      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:int   bean   inter   ref   jsp   url   app   pack   路径   

springmvc的注解开发

  1. 配置DispatcherServlet

    <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-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  2. spring配置文件springmvc-servlet.xml

    <?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">
    
        <!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理-->
        <context:component-scan base-package="cn.pinked.controller"/>
        <!--过滤默认的静态资源-->
        <mvc:default-servlet-handler/>
        <!--注解驱动-->
        <mvc:annotation-driven/>
        <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
    </beans>
  3. 控制器

    @Controller
    public class HelloController {
        //真实路径
        @RequestMapping("/hello")
        public String hello(Model model) {
            //封装数据
            model.addAttribute("msg", "hello springmvc annotation");
            //返回给视图解析器拼接
            return "hello";
        }
    
    }
  4. hello.jsp

springmvc的注解开发

标签:int   bean   inter   ref   jsp   url   app   pack   路径   

原文地址:https://www.cnblogs.com/pinked/p/12222776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!