zoukankan      html  css  js  c++  java
  • springmvc的注解开发

    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

  • 相关阅读:
    设计模式——单例模式
    设计模式——抽象工厂模式
    设计模式开篇——工厂模式
    Python编写工具Pycharm破解
    Maven的下载及安装
    如何在cmd命令行中运行Java程序
    聊聊BIO、NIO与AIO的区别
    Centos7查不出ip地址
    TensorFlow从1到2(六)结构化数据预处理和心脏病预测
    TensorFlow从1到2(五)图片内容识别和自然语言语义识别
  • 原文地址:https://www.cnblogs.com/pinked/p/12222776.html
Copyright © 2011-2022 走看看