目录

我这里主要讲的是非注解的映射器和适配器,入门可以看看,之前学的时候整理的笔记,现在发出来,项目由maven管理。

建立web项目

略。

需要的jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.notemi</groupId>
  <artifactId>springmvc-mybatis-maven</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>springmvc-mybatis-maven Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- spring version -->
    <spring.version>4.2.5.RELEASE</spring.version>
  </properties>

  <dependencies>
    <!-- spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- Spring AOP -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- AspectJ Runtime -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.6</version>
    </dependency>

    <!-- AspectJ Weaver -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.6</version>
    </dependency>

    <!-- SpringMVC -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- jstl 标签库 -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- servlet -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>

    <!-- jsp -->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.version}</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>springmvc-mybatis-maven</finalName>
  </build>
</project>

前端控制器配置

在WEB-INF\web.xml中配置前端控制器。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <!-- 1.配置前端控制器 -->
  <servlet>
    <servlet-name>springmvc-dispatcherservlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation配置springmvc加载的配置文件(处理器映射器、适配器等)-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/springmvc-dispatcherservlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc-dispatcherservlet</servlet-name>
    <!-- url-pattern 的三种配置方式:
    第一种:*.action  :访问.action结尾的资源时,由 DispatcherServlet 解析
    第二种:/ :访问所有资源都由 DispatcherServlet 解析,对于html、css、js、image等静态资源,需要配置不让 DispatcherServlet 解析
    第三种:/*  :注意这种方式错误,因为使用这种配置方式,最终咬转发到一个jsp页面时,仍然会由 DispatcherServlet 解析 jsp 地址,
                 不能根据jsp页面找到处理的handler,会报错。-->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

springmvc配置文件springmvc.xml

<?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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 5.配置视图解析器ViewResolver-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 4.配置controller -->
    <bean name="/userList" id="userList1" class="cn.notemi.controller.UserTestController"/>

    <!-- 3.处理映射器 -->
    <!-- 根据bean的name进行查找Handler 将action的url配置在bean的name中 -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

    <!-- 2.处理器适配器(需实现controller接口来开发) -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

</beans>

controller开发

package cn.notemi.controller;

import cn.notemi.po.User;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Flicker on 2017/1/17 0017.
 */
public class UserTestController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        //商品列表
        List<User> userList = new ArrayList<User>();

        User user1 = new User();
        user1.setId(1);
        user1.setUsername("张三");
        user1.setPassword("123");
        user1.setEmail("1@qq.com");

        User user2 = new User();
        user2.setId(2);
        user2.setUsername("李四");
        user2.setPassword("123");
        user2.setEmail("1@qq.com");

        userList.add(user1);
        userList.add(user2);

        //创建modelAndView准备填充数据、设置视图
        ModelAndView modelAndView = new ModelAndView();

        //填充数据
        modelAndView.addObject("userList", userList);
        //视图
        modelAndView.setViewName("user/index");

        return modelAndView;
    }
}

视图开发

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/1/17 0017
  Time: 下午 2:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table>
<c:forEach items="${userList}" var="userList">
<tr>
    <td>${userList.id}</td>
    <td>${userList.username}</td>
    <td>${userList.password}</td>
    <td>${userList.email}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

这里实现了controller接口来开发handler,现在不用这种方式来开发了,一般都使用注解,这个只是demo,供学习。

附件

springmvc-mybatis-maven-demo1.zip