目录

springmvc的注解开发必须掌握,现在都用注解了,也是一个很简单的示例,大家可以看一下,如果有问题联系我。

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>

    <!--&lt;!&ndash; 4.配置controller,非注解 &ndash;&gt;-->
    <!--<bean id="userListId" name="/userList" class="cn.notemi.controller.UserTestController"/>-->
    <!--<bean id="userListId2" class="cn.notemi.controller.UserTestController2"/>-->
    <!-- 4.扫描controller注解,多个包中间使用半角逗号分隔,注解 -->
    <context:component-scan base-package="cn.notemi.controller"/>

    <!--&lt;!&ndash; 3.处理映射器(非注解两种,可并存) &ndash;&gt;-->
    <!--&lt;!&ndash; (1)根据bean的name进行查找Handler 将action的url配置在bean的name中 &ndash;&gt;-->
    <!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />-->
    <!--&lt;!&ndash; (2)简单url映射 &ndash;&gt;-->
    <!--<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>-->

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

    <!--&lt;!&ndash; 3.注解映射器&ndash;&gt;-->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
    <!--&lt;!&ndash; 2.注解适配器 &ndash;&gt;-->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->

    <!-- 2/3使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
        mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上
        边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
        实际开发时使用mvc:annotation-driven
     -->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

controller开发

package cn.notemi.controller;

import cn.notemi.po.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

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.
 */
@Controller
public class UserTestController{
    @RequestMapping(value = "/user")
    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>
</table>
</c:forEach>
</body>
</html>

附件:springmvc-mybatis-maven-demo注解1.zip