Spring MVC配置

Spring MVC流程图

这里写图片描述

1,导入包

【spring】
—20个全部导入
—依赖包commons-logging-1.1.1.jar
【JSTL】
— Jstl.jar、standard.jar
— springmvc默认使用jstl显示视图,如果使用jstl把这两个包加上 也可以不加)

下载地址:http://download.csdn.net/detail/qq_24082497/9635147

2,创建实体类,jsp文件

Category.java

package com.po;

public class Category {

    private int id;
    private String cname;
    private String cdesc;

    public Category() {
        super();
    }

    public Category(int id, String cname, String cdesc) {
        super();
        this.id = id;
        this.cname = cname;
        this.cdesc = cdesc;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getCdesc() {
        return cdesc;
    }

    public void setCdesc(String cdesc) {
        this.cdesc = cdesc;
    }

}

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
    <a href="cate.action">查询所有</a>
</body>
</html>

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'list.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
    <center>
        <h1>商品种类管理</h1>
    </center>
    <table id="table1" width="80%">
        <tr>
            <th>商品种类编号</th>
            <th>商品种类名称</th>
            <th>商品描述</th>
            <th>操作</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${requestScope.list}" var="category">
            <tr>
                <td>${category.id}</td>
                <td>${category.cname}</td>
                <td>${category.cdesc}</td>
                <td><a href="CategoryServlet?action=delete&cid=${category.id}"
                    onclick="return confirm('您确认删除吗??');">删除</a></td>
                <td><a
                    href="CategoryServlet?action=getOneForUpdate&cid=${category.id}">修改</a></td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

3,src下配置springmvc.xml

在src下新建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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


 </beans>

4,配置web.xml

在web.xml中配置中心控制器 DispatcherServlet

功能: 分发请求到Handler(控制器), 并最终给客户端做出相应

<servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 如果不配置contextConfigLocation 默认读取的是 WEB-INF/servlet名-servlet.xml(也就是springmvc-servlet.xml) 这个文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
<servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- *.action  客户端发出*.action的请求时 被控制器处理 -->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

5,编写Handler

需要实现Controller接口的handleRequest()方法,才能由
SimpleControllerHandlerAdapter执行

package com.controller;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.po.Category;

public class MyController implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        ModelAndView modelAndView = new ModelAndView();
        // 一.填充数据
        // 二.调用业务逻辑
        // 需求: 查询所有商品种类
        List<Category> list = new ArrayList<Category>();

        Category category1 = new Category(1, "手机", "通讯工具");
        Category category2 = new Category(2, "图书音像", "通讯工具");
        Category category3 = new Category(3, "服装鞋帽", "生活必需");

        list.add(category1);
        list.add(category2);
        list.add(category3);

        // 三.转发视图
        // 相当于request.setAttribute("list", list);
        modelAndView.addObject("list", list);
        modelAndView.setViewName("list.jsp");
        return modelAndView;
    }

}

6,配置适配器和Controller

<!-- 配置处理器 适配器 -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>


<!-- 配置映射关系 -->
<bean name="/category/cate.action" class="com.controller.MyController" />

不配置映射器和适配器也行,为什么 ?
如果不配置默认使用org.springframework.web.servlet.DispatcherServlet.properties 文件中配置的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值