zoukankan      html  css  js  c++  java
  • springmvc入门demo

    目录结构:

     1 package com.wyl;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 /**
     6  * 
     7  * @author Wei
     8  * @time  2016年12月3日 下午1:12:07
     9  */
    10 @Controller
    11 public class HelloWorld {
    12 
    13     public HelloWorld() {
    14         System.out.println("----HelloWorld.java 的 构造器-------");
    15     }
    16 
    17     /**
    18      * 映射控制
    19      * 使用RequestMapping注解配置需要进行拦截的request,并且提供返回的资源内容
    20      * @return String
    21      */
    22     @RequestMapping("/helloworld")
    23     public String hello() {
    24         System.out.println("----------");
    25         return "success";
    26     }
    27 }

    springmvc-servlet.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
     8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
     9 
    10     <!--  
    11         需要进行 Spring 整合 SpringMVC 吗 ?
    12         还是否需要再加入 Spring 的 IOC 容器 ?
    13         是否需要再 web.xml 文件中配置启动 Spring IOC 容器的 ContextLoaderListener ?
    14         
    15         1. 需要: 通常情况下, 类似于数据源, 事务, 整合其他框架都是放在 Spring 的配置文件中(而不是放在 SpringMVC 的配置文件中).
    16         实际上放入 Spring 配置文件对应的 IOC 容器中的还有 Service 和 Dao. 
    17         2. 不需要: 都放在 SpringMVC 的配置文件中. 也可以分多个 Spring 的配置文件, 然后使用 import 节点导入其他的配置文件
    18     -->
    19     
    20     <!--  
    21         问题: 若 Spring 的 IOC 容器和 SpringMVC 的 IOC 容器扫描的包有重合的部分, 就会导致有的 bean 会被创建 2 次.
    22         解决:
    23         1. 使 Spring 的 IOC 容器扫描的包和 SpringMVC 的 IOC 容器扫描的包没有重合的部分. 
    24         2. 使用 exclude-filter 和 include-filter 子节点来规定只能扫描的注解
    25     -->
    26     
    27     <!--  
    28         SpringMVC 的 IOC 容器中的 bean 可以来引用 Spring IOC 容器中的 bean. 
    29         返回来呢 ? 反之则不行. Spring IOC 容器中的 bean 却不能来引用 SpringMVC IOC 容器中的 bean!
    30     -->
    31     
    32     <context:component-scan base-package="com.wyl" use-default-filters="false">
    33         <context:include-filter type="annotation" 
    34             expression="org.springframework.stereotype.Controller"/>
    35         <context:include-filter type="annotation" 
    36             expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    37     </context:component-scan>
    38 
    39     <!-- 配置视图解析器 -->
    40     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    41         <property name="prefix" value="/WEB-INF/views/"></property>
    42         <!-- <property name="suffix" value=".jsp"></property> -->
    43         <property name="suffix" value=".jsp"></property>
    44     </bean>
    45     
    46     <mvc:default-servlet-handler/>
    47     <mvc:annotation-driven></mvc:annotation-driven>
    48     
    49 </beans>

    web.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns="http://java.sun.com/xml/ns/javaee"
     4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     5     id="WebApp_ID" version="2.5">
     6     <display-name>springmvc011</display-name>
     7     <welcome-file-list>
     8         <welcome-file>index.html</welcome-file>
     9         <welcome-file>index.jsp</welcome-file>
    10         <welcome-file>default.html</welcome-file>
    11     </welcome-file-list>
    12     <!-- <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    13         <init-param> <param-name>contextConfiguration</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> 
    14         </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> 
    15         <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> -->
    16     <!-- 配置 DispatcherServlet -->
    17     <servlet>
    18         <servlet-name>springmvc</servlet-name>
    19         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    20         <!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->
    21         <!-- 实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的. 默认的配置文件为: 
    22             /WEB-INF/<servlet-name>-servlet.xml -->
    23         <!-- <init-param> 
    24                 <param-name>contextConfigLocation</param-name> 
    25                 <param-value>classpath:springmvc-servlet2.xml</param-value> 
    26             </init-param> -->
    27         <load-on-startup>1</load-on-startup>
    28     </servlet>
    29     <servlet-mapping>
    30         <servlet-name>springmvc</servlet-name>
    31         <url-pattern>/</url-pattern>
    32     </servlet-mapping>
    33 </web-app>

     效果:

  • 相关阅读:
    7. Spring验证、数据绑定和类型转换
    J2EE应用与移动互联网-写在前头
    IT基础设施资源的实践----写在前头
    JavaScript随笔记(一)基础概念以及变量类型
    js函数表达式
    js面形对象(2)
    js面向对象
    viPlugin安装破解
    Ubuntu12.04 使用中遇到的问题
    关于sizeof
  • 原文地址:https://www.cnblogs.com/Sunnor/p/6128520.html
Copyright © 2011-2022 走看看