首页 > 代码库 > SpringMVC 控制器(一)

SpringMVC 控制器(一)

这一篇讲以下三个知识点:

1.@RequestMapping——请求映射注解

2.@RequestParam——请求参数

3.ModelAndView——返回模型和视图

示例:(学生信息的查询、添加、修改)——为了方便就不链接数据库直接在类中模拟了一些数据

1.配置springMVC的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC2</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 同struts一样,也是需要拦截请求 -->
  <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern><!-- 拦截所有以.do结尾的请求 -->
    </servlet-mapping>
</web-app>

2.配置spring-mvc.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

    <!-- 使用注解的包,包括子集 -->
    <context:component-scan base-package="com.maya"/>

    <!-- 视图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" /><!-- 返回视图到这个目录下 -->
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

3.建立model层,实体类student

4.建立controller层,StudentController类

package com.maya.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;//是web.servlet.ModelAndView!!!不要选错

import com.maya.model.Student;

@Controller//声明这是控制层
@RequestMapping("/student")//为了区分模块,在请求时通常会在模块前加一个前缀
public class StrudentController {
    
    private static List<Student> studentList=new ArrayList<Student>();
    static{//模拟数据
        studentList.add(new Student(1, "张三", 11));
        studentList.add(new Student(2, "李四", 12));
        studentList.add(new Student(3, "王五", 13));
    }
    
    @RequestMapping("/list")//定义请求的名字
    public ModelAndView list(){//ModelAndView返回模型和视图
        ModelAndView mav=new ModelAndView();
        mav.addObject("studentList", studentList);
        mav.setViewName("student/list");//设置视图的名,student文件名下的list.jsp
        return mav;
    }
    
    @RequestMapping("/preSave")                            
    public ModelAndView preSave(@RequestParam(value="http://www.mamicode.com/id",required=false) String id){
                                //接收前台参数id,因为添加修改都用这一个请求,那么required应设为false,后面定义接收参数的类型
        ModelAndView mav=new ModelAndView();
        if(id==null){//如果没有接收到参数,说明是添加
            mav.setViewName("student/add");//设置视图的名,student文件名下的add.jsp
        }else{//否则就是修改了
            mav.addObject("student", studentList.get(Integer.parseInt(id)-1));
            mav.setViewName("student/update");//设置视图的名,student文件名下的update.jsp
        }        
        return mav;
    }
}

5.建立view视图层,因为设定了在jsp文件夹下的student文件下!所以要相应的建立正确的路径

技术分享

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/student/preSave.do">添加</a>
<table>
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>操作</th>
    </tr>
<c:forEach var="list" items="${studentList }">
    <tr>
        <td>${list.id }</td>
        <td>${list.name }</td>
        <td>${list.age }</td>
        <td><a href="${pageContext.request.contextPath }/student/preSave.do?id=${list.id }">修改</a></td>
        <td><a href="delete.do?id=${list.id }">删除</a></td>
    </tr>    
</c:forEach>
</table>
</body>
</html>

 

SpringMVC 控制器(一)