zoukankan      html  css  js  c++  java
  • 10SpringMvc_springmvc快速入门小案例(注解版本)

    第一步:新建案例工程:

                

    这个案例用到的是1.com.guigu.shen.Action的包里面的文件(Helloaction.java是控制类,springmvc_005.xml是springmvc的配置文件)。2.springmvc.xml。3.index.xml。4.web.xml。5.jsp/success.jsp

    第一步:先写web.xml;

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>SpringMvc_10day_self</display-name>
      <!-- Spring提供了一个Filter专门用来解决Post提交中文的乱码问题 -->
       <filter>
         <filter-name>CharacterEncodingFilter</filter-name>
         <filter-class>
     org.springframework.web.filter.CharacterEncodingFilter
     </filter-class>
     <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
     </init-param>
    </filter>
     <filter-mapping>
     <filter-name>CharacterEncodingFilter </filter-name>
     <url-pattern>/*</url-pattern>
     </filter-mapping>
    
     
    
     
     
      <servlet>
      <!--这个名字可以随便取得,但是这个名字取了之后,以后在 WEB-INF下面创建SpirngMVC的配置文件是,命名必须以这个开头,
      
      所以这里取名叫做DispatcherServlet,那么之后的xml文件取名必须为DispatcherServlet-servlet.xml(一个字都不能差)
      
      -->
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!-- 通知DispatcherServlet去指定目录下找到springmvc.xml配置文件 -->
     <!-- 
     注意这里的  <param-name>contextConfigLocation</param-name>一个字母都不能有错
     一旦有错就会去WEB-INF下面去找
      -->
              <init-param>
                   <param-name>contextConfigLocation</param-name>
                  <param-value>classpath:spring.xml</param-value>
              </init-param>
     </servlet>
     <servlet-mapping>
       <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
     
     </servlet-mapping>
    
     
      <welcome-file-list>
       
        <welcome-file>index.jsp</welcome-file>
      
      </welcome-file-list>
    </web-app>

    第二步:写spring.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:aop="http://www.springframework.org/schema/aop"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          xsi:schemaLocation="
          
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
           
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
          
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    >
    <import resource="com/guigu/shen/Action5/springmvc_005.xml"/>
    </beans>

    第三步:写com/guigu/shen/Action5/springmvc_005.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:aop="http://www.springframework.org/schema/aop"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          xsi:schemaLocation="
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    >
         <!-- 控制器(程序员)(必须配置) -->
    <context:component-scan base-package="com.guigu.shen.Action5"/>
     <!-- <bean name="/hello.action" class="com.guigu.shen.Action5.HelloAction"></bean> --> 
    
      <!-- 基于注解的映射器(可选) 
      这个类和以前的xml方式的类不同,专门是注解用的
      -->
          <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
          
          <!-- 基于注解的适配器(可选)
            这个类和以前的xml方式的类不同,专门是注解用的
           -->
          <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
          
          <!-- 视图解析器(可选) 
            这个类和以前的xml方式的类一样
          -->
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
    
    
    </beans>

    第四步:写HelloAction.java控制器。

    package com.guigu.shen.Action5;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /*
     * 单例的控制器(由程序员去写)
     * 以前我们写控制的做法是implends Controller接口,
     * 或者extends Abs...抽象类
     * 现在我不想这么写了,用注解来做,写一个@Controller这样就代表这个是一个控制器。这样解析这个类
     * 时就知道他是控制器了。
     * 
     * 
     */
    @Controller
    public class HelloAction {
    
        public HelloAction() {
                  System.out.print(this.hashCode());
        }
      



    /* * * @RequestMapping(value="/hello")表示这个方法是用来响应/hello.action请求的 * 按住alt+/就是跳出来注解里面的参数。只要是/hello.action的请求都会交给HelloAction中的 * helloMethod方法来处理。 * */
    @RequestMapping(value="/hello") public String helloMethod(Model model) throws Exception { System.out.println("Hello:helloMethod()"); //往Model中封装数据。 model.addAttribute("message", "这是hello请求"); return "jsp/success.jsp"; }








    /* * * @RequestMapping(value="/bey.action")表示这个方法是用来响应/bey.action请求的 * 按住alt+/就是跳出来注解里面的参数。只要是/bey.action的请求都会交给HelloAction中的 * beyMethod方法来处理。 * */ @RequestMapping(value="/bey.action") //@RequestMapping(value="/hello") public String beyMethod(Model model) throws Exception { System.out.println("Hello:helloMethod()"); //往Model中封装数据。 model.addAttribute("message", "这是bye请求"); return "jsp/success.jsp"; } }

    第五步:编写success.jsp页面

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
        Success. <br>
        ${requestScope.message} 
         
      </body>
    </html>

    最后测试一下:输入http://127.0.0.1:8080/SpringMvc_10day_self/bey.action

                            结果:跳转到success.jsp 并输出“这是bye请求”内容。

                        输入http://127.0.0.1:8080/SpringMvc_10day_self/hello.action

                          结果:跳转到success.jsp 并输出“这是hello请求”内容。

    对上面的案例流程说明一下:

    1.浏览器发出请求,比如hello.action

    2服务器根据web.xml的配置来到了DispatcherServlet.并且DispatcherServlet把这个请求给了映射器,映射器会去springmvc_005.xml中去寻找控制器类,到了那里发现了

    <context:component-scan base-package="com.guigu.shen.Action5"/>知道了这是注解,所以马上去"com.guigu.shen.Action5"包下面去寻找有没有被

    @Controller注释过的类,发现有的,然后找到了HelloAction类。然后去这个类里面去找被注解了@RequestMapping(value="/hello")的方法,然后找到了helloMethod(Model model)方法然后去执行这个方法。执行好之后返回一个Model,然后把这个Model交给视图解析器去解析,解析好之后在交给DispatcherServlet,最后DispatcherServlet根据解析出来的网址跳转到success.jsp 并输出“这是hello请求”内容。

    问题:

    1.刚开始的时候我在springmvc_005.xml是这么配置的。

    <context:component-scan base-package="com.guigu.shen.Action5"/>
    <bean name="/hello.action" class="com.guigu.shen.Action5.HelloAction"></bean> 
    这两个一起配置,然后就会出错,说是重复了。你想啊第一句是通过注解的方式去找了。第二句直接通过xml的方式去找了,相当于去找了两次,当然会冲突啊。



    2. 把<context:component-scan base-package="com.guigu.shen.Action5"/>去掉。
    保留<bean name="/hello.action" class="com.guigu.shen.Action5.HelloAction"></bean>
    我在想为什么也会成功啊?照理说,我在HelloAction类里面的方法也是用注解去配的啊,我都把注解那句去掉了,保留了xml的方式,之前次采用xmL的方式时,里面的
    执行方法是有特定写法的?答案:<context:component-scan base-package="com.guigu.shen.Action5"/>这句只是告诉系统前去com.guigu.shen.Action5这个包里面寻找
    被@Contolrr注解过的类。至于里面的 @RequestMapping(value="/hello") 这些注解当然可以使用啊。然后只是把寻找里面控制类的任务直接由
    <bean name="/hello.action" class="com.guigu.shen.Action5.HelloAction"></bean>去完成了罢了。并不影响 @RequestMapping(value="/hello")注解的寻找。
     
  • 相关阅读:
    lipo命令
    Eclipse安装ADT插件
    编译vo-aacenc遇到的问题
    双MIC安卓手机录音问题
    天天动听MP3解码器性能提升50%
    Sublime Text 2结合VS2010配置C C++编译
    讯飞语音语义接口测试
    讯飞语音接口注册
    iOS阶段学习第31天笔记(UINavigationBar介绍)
    iOS App上架AppStore 会遇到的坑
  • 原文地址:https://www.cnblogs.com/shenxiaoquan/p/5750614.html
Copyright © 2011-2022 走看看