zoukankan      html  css  js  c++  java
  • 《SSM框架搭建》三.整合spring web

    感谢学习http://blog.csdn.net/zhshulin/article/details/37956105#,还是修改了spring到最新的版本和接口开发示例

    根据前一篇日志,已经有了mybatis3+spring4的maven项目,在这基础上继续整合

    1,首先下载需要的jar包,这里使用了全局参数,并添加以下jar包,(这里还抛出过异常找不到fasterxml/jackson/core/JsonProcessingException,在maven库也找了蛮久

    <properties>
            <!-- spring版本号 -->
            <spring.version>4.3.2.RELEASE</spring.version>
            <!-- mybatis版本号 -->
            <mybatis.version>3.2.8</mybatis.version>
            <!-- log4j日志文件管理包版本 -->
            <slf4j.version>1.7.7</slf4j.version>
            <log4j.version>1.2.17</log4j.version>
        </properties        <!-- 添加整合spring web需要的包 -->
        <!-- 添加整合spring web需要的包 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-oxm</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- 格式化对象,方便输出日志 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.1.41</version>
            </dependency>
    
            <!-- 日志文件管理包 -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <!-- log end -->
            <!-- 引入JSON -->
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
                <version>1.9.13</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.8.2</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.2</version>
            </dependency>
            <!-- 上传组件包 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3.1</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.4</version>
            </dependency>
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.9</version>
            </dependency>

    2.添加配置文件spring-mvc.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:p="http://www.springframework.org/schema/p"  
        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-4.3.xsd    
                            http://www.springframework.org/schema/context    
                            http://www.springframework.org/schema/context/spring-context-4.3.xsd    
                            http://www.springframework.org/schema/mvc    
                            http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">  
        <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->  
        <context:component-scan base-package="com.controller" />  
        <!--避免IE执行AJAX时,返回JSON出现下载文件 -->  
        <bean id="mappingJacksonHttpMessageConverter"  
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
            <property name="supportedMediaTypes">  
                <list>  
                    <value>text/html;charset=UTF-8</value>  
                </list>  
            </property>  
        </bean>  
        <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->  
        <bean  
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
            <property name="messageConverters">  
                <list>  
                    <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->  
                </list>  
            </property>  
        </bean>  
        <!-- 定义跳转的文件的前后缀 ,视图模式配置-->  
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
            <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->  
            <property name="prefix" value="/WEB-INF/jsp/" />  
            <property name="suffix" value=".jsp" />  
        </bean>  
          
        <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->  
        <bean id="multipartResolver"    
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
            <!-- 默认编码 -->  
            <property name="defaultEncoding" value="utf-8" />    
            <!-- 文件大小最大值 -->  
            <property name="maxUploadSize" value="10485760000" />    
            <!-- 内存中的最大值 -->  
            <property name="maxInMemorySize" value="40960" />    
        </bean>   
      
    </beans>  

    3修改web.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    
        <listener>
            <description>Spring监听器</description>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- ContextLoaderListener初始化Spring上下文时需要使用到的contextConfigLocation参数 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <!-- 配置spring.xml和spring-mybatis.xml这两个配置文件的位置,固定写法 -->
            <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
        </context-param>
    
        <!-- 编码过滤器 -->  
        <filter>  
            <filter-name>encodingFilter</filter-name>  
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
            <async-supported>true</async-supported>  
            <init-param>  
                <param-name>encoding</param-name>  
                <param-value>UTF-8</param-value>  
            </init-param>  
        </filter>  
        <filter-mapping>  
            <filter-name>encodingFilter</filter-name>  
            <url-pattern>/*</url-pattern>  
        </filter-mapping>  
        <!-- 防止Spring内存溢出监听器 -->  
        <listener>  
            <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
        </listener>  
        <!-- Spring MVC servlet -->  
        <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:spring-mvc.xml</param-value>  
            </init-param>  
            <load-on-startup>1</load-on-startup>  
            <async-supported>true</async-supported>  
        </servlet>  
        <servlet-mapping>  
            <servlet-name>SpringMVC</servlet-name>  
            <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->  
            <url-pattern>/</url-pattern>  
        </servlet-mapping>  
    </web-app>

    4.在controller包下创建类UserController

    package com.controller;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.model.User;
    import com.service.UserServiceI;
    
    @Controller
    @RequestMapping("/user")
    public class UserController {
        @Resource
        private UserServiceI userService;
    
        /**
         * 跳转页面测试
         */
        @RequestMapping("/showUser")
        public String toIndex(HttpServletRequest request, Model model) {
            String userId = request.getParameter("id");
            User user = this.userService.getUserById(userId);
            model.addAttribute("user", user);
            return "showUser";
        }
    
        /**
         * 一般接口
         */
        @RequestMapping(value = "/getUserById", method = RequestMethod.GET)
        @ResponseBody
        public Object getUserById(HttpServletRequest request, Model model) {
            String userId = request.getParameter("id");
            User user = this.userService.getUserById(userId);
            model.addAttribute("user", user);
            return user.getUserName();
        }
    }

    这里有可能提示访问受限@Resource上,提示The type Resource is not accessible due to restriction on required library

    点击项目属性preferences>java build path>把右侧【libraries中的JRE System Library】删除重新导入即可

    5.调用普通接口测试结果,这里结果应该需要写一个封装对象。稍后补充

    6.页面跳转接口需要创建对应的jsp页面

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    <html>  
      <head>  
        <title>测试</title>  
      </head>  
        
      <body>  
        ${user.userName}
      </body>  
    </html>  

    测试结果

     完成之后修改了mapper文件包位置,移入resources文件夹,修改对应的配置文件中的路径,

  • 相关阅读:
    SpringBoot自动装配原理解析
    面试官:你知道Spring中有哪些可以让我们扩展的地方么
    Spring事件监听机制
    Spring扩展点之Aware接口族
    程序员的进阶课-架构师之路(13)-B-树
    程序员的进阶课-架构师之路(12)-2-3-4树
    程序员的进阶课-架构师之路(11)-最容易理解的红黑树
    程序员的进阶课-架构师之路(10)-霍夫曼树
    程序员的进阶课-架构师之路(9)-平衡二叉树(AVL树)
    程序员的进阶课-架构师之路(8)-二叉树
  • 原文地址:https://www.cnblogs.com/Ebird/p/5940951.html
Copyright © 2011-2022 走看看