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">
    <!-- 配置SpringMVC核心Servlet -->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 初始化参数 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-*.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <!-- 匹配所有url -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring-mvc.xml

配置SpringMVC的配置基于注解的依赖注入,以及视图解析模型

<?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: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">
    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <mvc:annotation-driven/>

    <!-- 因为我们的Servlet的mapping为"/",这里为了解决静态资源的处理:js,gif,png -->
    <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>

    <!-- 自动扫描web包下的所有类,如果@Controller注入为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<User> 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>Title</title>
</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>

整合测试

运行项目:

在浏览器输入:http://localhost:8080/SSM/user/list

说明整合成功。

源码下载地址: https://github.com/cskun/SSM.git

你可能感兴趣的:(spring,spring,mvc,mybatis)