Build Spring + SpringMVC + Mybatis framework (integrate Spring, Mybatis and Spring MVC)

(1) Placement spring-mvc.xml

Mainly auto scan controller, view mode.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:p="http://www.springframework.org/schema/p"
  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-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
     
    <!-- Automatically scan the package, so that SpringMVC thinks that the class annotated with @controller under the package is a controller -->
    <context:component-scan base-package="com.aheizi.controller"/>
    
    <!-- Prevent IE from returning json to download during ajax request-->
    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    
         <property name="supportedMediaTypes">
             <list>
                 <value>text/html;charset=UTF-8</value>
             </list>
         </property>
     </bean>
     
    <!-- Define the prefix and suffix of the jumped file, view mode configuration -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- My understanding of the configuration here is to automatically add a prefix and suffix to the return string of the action method to become a usable url address-->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
</beans>

(2) Configure Web.xml

Contains spring and mybatis configuration files, coding filters, logging, spring listeners, spring memory overflow prevention listeners, springmvc core configuration and error jump pages.

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>spring_mybatis_springmvc</display-name>
    
    <!-- Spring and mybatis configuration files -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/spring-mybatis.xml</param-value>
    </context-param>
    
    <!-- encoding filter -->
    <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>
    
    <!-- logging (must precede ContextLoaderListener) -->
    <context-param>
        <!-- log configuration file path-->
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:properties/log4j.properties</param-value>
    </context-param>
    <context-param>
        <!-- The refresh interval of the log page -->
        <param-name>log4jRefreshInterval</param-name>
        <param-value>6000</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    
    <!-- Spring Listener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- Prevent Spring memory overflow listener -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
 
    <!-- Spring MVC core configuration-->
    <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>
    
    <!-- suffix -->
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
    <!-- Welcome Page-->
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
    
    <!-- Error jump page-->
    <error-page>
        <!-- Incorrect path-->
        <error-code>404</error-code>
        <location>/WEB-INF/errorpage/404.html</location>
    </error-page>
    <error-page>
        <!-- No access, access forbidden -->
        <error-code>405</error-code>
        <location>/WEB-INF/errorpage/405.html</location>
    </error-page>
    <error-page>
        <!-- Internal error -->
        <error-code>500</error-code>
        <location>/WEB-INF/errorpage/500.html</location>
    </error-page>
    
</web-app>
(3) Create the showUser.jsp page
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Index</title>
</head>
<body>
        <h3>hello</h3><br/>
        ${user.userName}
</body>
</html>

That's ok

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325873278&siteId=291194637