SSM框架集成(二)springmvc的集成

完成上一篇博客的内容之后,就可以集成springmvc了,配置springmvc比较简单,我们需要在resouces里面再建一个文件,这里叫spring-mvc.xml,目录结构如下:

SSM框架集成(二)springmvc的集成_第1张图片

spring-mvc.xml内容


<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-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
    <context:component-scan base-package="com.czh.controller" />
    
    <mvc:annotation-driven />
    
    <mvc:resources mapping="/static/**" location="/static/" />
    <mvc:resources mapping="/static/image/**" location="/static/image/" />
    
    <bean id="fastJsonHttpMessageConverter"
          class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        
        <property name="supportedMediaTypes">
            <list>
                
                <value>text/html;charset=UTF-8value>
                <value>application/json;charset=UTF-8value>
            list>
        property>
    bean>
    
    <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="fastJsonHttpMessageConverter" /> 
            list>
        property>
    bean>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix" value="/WEB-INF/views/" />
        <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>

大家可以思考一下,在上一篇博客里,为什么Junit可以打开spring容器,来实现依赖注入的功能?
显然,是因为在测试类上我们贴了一个注解

SSM框架集成(二)springmvc的集成_第2张图片

告诉编辑器,先去找到那个文件,在junit之外呢?我们该怎么去提醒编辑器先去扫描我们的配置文件呢?难道要在每一个controller里面添加类似于这样的注解吗?
当然不是。在我们生成web项目的时候,在WEB-INF下面有个web.xml文件

SSM框架集成(二)springmvc的集成_第3张图片

在这里,我们就可以配置,对于某些特定的请求,会先去扫描我们的配置文件。这里就简单说一些,想要了解的可以自行百度。

web.xml


<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_3_0.xsd"
         version="3.0">
  <display-name>Archetype Created Web Applicationdisplay-name>
  
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:ApplicationContext.xmlparam-value>
  context-param>
  
  <filter>
    <filter-name>encodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <async-supported>trueasync-supported>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>encodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
  listener>

  
  <servlet>
    <servlet-name>SpringMVCservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:spring-mvc.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
    <async-supported>trueasync-supported>
  servlet>
  
  <servlet-mapping>
    <servlet-name>SpringMVCservlet-name>
    
    <url-pattern>/url-pattern>
  servlet-mapping>
  <welcome-file-list>
    <welcome-file>/index.jspwelcome-file>
  welcome-file-list>

web-app>

然后去写一个controller和一个页面就可以发布项目了。
controller

package com.czh.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("tasklist")
public class TaskListController {
    @RequestMapping("index")
    public String index(){
        return "index";
    }

}

jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
    <h1>hello worldh1>
body>
html>

发布项目,访问localhost/tasklist/index就可以了。我的tomcat端口号是80,可以缺省不写。
SSM框架集成(二)springmvc的集成_第4张图片

你可能感兴趣的:(java框架)