SSM之WEB整合SpringMVC

前面我们已经做了SSM之Service层基于注解的声明式事物的设计,如果还没有整合,那么请参考:
http://blog.csdn.net/uq_jin/article/details/51541971

整合完成图

SSM之WEB整合SpringMVC_第1张图片

web.xml

配置SpringMVC的核心Servlet,并加载配置文件

<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,只有一个测试方法

@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文件。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="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

你可能感兴趣的:(java-web)