目录

springmvc的非注解开发简单示例。

接着上一篇:http://notemi.cn/springmvc---non-annotation-development-1.html

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 id="userListId" name="/userList" class="cn.notemi.controller.UserTestController"/>
    <bean id="userListId2" class="cn.notemi.controller.UserTestController2"/>

    <!-- 3.处理映射器(非注解两种,可并存) -->
    <!-- (1)根据bean的name进行查找Handler 将action的url配置在bean的name中 -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
    <!-- (2)简单url映射 -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/userList1">userListId</prop>
                <prop key="/userList2">userListId</prop>
                <prop key="/userList3">userListId2</prop>
            </props>
        </property>
    </bean>

    <!-- 2.处理器适配器(非注解的两种) -->
    <!-- (1)要求handler实现controller接口-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!-- (2)要求handler实现HttpRequestHandler接口 -->
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

</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;
    }
}
package cn.notemi.controller;

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

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

/**
 * Created by Flicker on 2017/1/17 0017.
 */
public class UserTestController2 implements HttpRequestHandler {

    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //商品列表
        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);

        //填充数据
        request.setAttribute("userList", userList);
        //视图
        request.getRequestDispatcher("/WEB-INF/pages/user/index.jsp").forward(request,response);
        //从上边可以看出此适配器器的handleRequest方法没有返回ModelAndView,可通过response修改定义响应内容,比如返回json数据:
        //response.setCharacterEncoding("utf-8");
        //response.setContentType("application/json;charset=utf-8");
        //response.getWriter().write("json串");
    }
}

视图开发

<%--
  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>

附件:springmvc-mybatis-maven-demo2.zip