SSMのWEB統合SpringMVC

14028 ワード

前にSSMのService層の注釈に基づく宣言的なものの設計を行いましたが、まだ統合されていない場合は、以下の参照を参照してください.http://blog.csdn.net/uq_jin/article/details/51541971
統合完了図
SSM之WEB整合SpringMVC_第1张图片
web.xml
SpringMVCのコアサーブレットを構成し、プロファイルをロード
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1"
         metadata-complete="true">
    
    <servlet>
        <servlet-name>DispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring/spring-*.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        
        <url-pattern>/url-pattern>
    servlet-mapping>
    
    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
web-app>

spring-mvc.xml
SpringMVCの構成注釈依存注入とビュー解析モデルに基づく

<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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.1.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    
    <mvc:annotation-driven/>

    
    <mvc:default-servlet-handler/>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    bean>

    
    <context:component-scan base-package="me.jinkun.ssm.web"/>
beans>

UserController .java
ここではweb層のControllerですが、テスト方法は1つしかありません.
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService mUserService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        List userList = mUserService.findAll();
        model.addAttribute("list", userList);
        return "list";
    }
}

list .jsp
Listを表示するためのビューjspファイル.
"text/html;charset=UTF-8" language="java" %>
"c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
    <table  border="1" align="center" width="30%">
        <tr >
            <td>td>
            <td>td>
        tr>
        <c:forEach items="${list}" var="user">
            <tr><td>${user.username}td><td>${user.userpass}td>tr>
        c:forEach>
    table>
body>
html>

統合テスト
プロジェクトの実行:
SSM之WEB整合SpringMVC_第2张图片
ブラウザで入力:http://localhost:8080/SSM/user/list
SSM之WEB整合SpringMVC_第3张图片
統合に成功したことを示します.
ソースのダウンロードアドレス:https://github.com/cskun/SSM.git