springmvc环境搭建

springmvc环境搭建

数据库:

建立好业务数据存储数据库。

Java环境:

jdk1.7、MyEclipse14、数据库MySQL 5.6、spring版本:4.2.1

功能需求

商品列表查询

第一步:

创建动态web工程,导入spring的所有jar,注意一定要包含spring-webmvc-4.2.1.RELEASE.jar

日志:commons-logging-1.1.1.jar

jsp标签库:jstl.jar

连接数据库:数据库连接驱动

创建一个资源文件夹,并在资源文件夹中创建springmvc.xml文件

这里写图片描述

第二步:

配置前端控制器

在web.xml中配置前端控制器


<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    
  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    <init-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:springmvc.xmlparam-value>
    init-param>
  servlet>

  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>*.actionurl-pattern>
  servlet-mapping>

web-app>

配置处理器映射器

在classpath:springmvc.xml中配置处理器映射器

BeanNameUrl:将bean的name作为url查找,需要在配置Handler是指定name(url)
        -->
         class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

配置处理适配器

 
         <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

配置视图解析器

  
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />

编写处理器Handler

package com.ld.springmvc.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.ld.springmvc.po.Items;


public class ItemsController implements Controller{
    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        //使用静态数据模拟
        List itemsList = new ArrayList();
        Items item = new Items();
        item.setName("旺仔");
        item.setPrice(10f);

        Items item1 = new Items();
        item1.setName("旺1仔");
        item1.setPrice(11f);

        Items item2 = new Items();
        item2.setName("旺仔");
        item2.setPrice(10f);

        Items item3 = new Items();
        item3.setName("旺2仔");
        item3.setPrice(10f);

        itemsList.add(item);
        itemsList.add(item1);
        itemsList.add(item2);
        itemsList.add(item3);

        //返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        //类似于request的setAttribute
        modelAndView.addObject("itemsList", itemsList);
        //指定视图

        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        return modelAndView;
    }

}

配置处理器Handler


   <bean name="queryItem.action" class="com.ld.springmvc.controller.ItemsController" />

Handler需要实现Controller接口

package com.ld.springmvc.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.ld.springmvc.po.Items;


public class ItemsController implements Controller{

    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        //使用静态数据模拟
        List itemsList = new ArrayList();
        Items item = new Items();
        item.setName("旺仔");
        item.setPrice(10f);

        Items item1 = new Items();
        item1.setName("旺1仔");
        item1.setPrice(11f);

        Items item2 = new Items();
        item2.setName("旺仔");
        item2.setPrice(10f);

        Items item3 = new Items();
        item3.setName("旺2仔");
        item3.setPrice(10f);

        itemsList.add(item);
        itemsList.add(item1);
        itemsList.add(item2);
        itemsList.add(item3);

        //返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        //类似于request的setAttribute
        modelAndView.addObject("itemsList", itemsList);
        //指定视图

        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        return modelAndView;
    }

}

编写视图(itemsList.jsp)

<%@ page language="java" import="java.util.*" contentType="text-html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">

    <title>查询商品列表title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    

  head>

  <body>
    <form action="${pageContext.request.contextPath }/item/queryItem.action" method="post">
        查询条件
        <table width="100%" border="1">
            <tr>
                <td><input type="submit" value="查询" />td>
            tr>
        table>
        商品列表:
        <table width="100%" border="1">
            <tr>
                <td>商品名称td>
                <td>商品价格td>
                <td>生产日期td>
                <td>商品描述td>
                <td>操作td>
            tr>
            <c:forEach items="${itemsList }" var="item">
            <tr>
                <td>${item.name }td>
                <td>${item.price }td>
                <td><fmt:formatDate value="${item.createtime }" pattern="yyyy-MM-dd HH:mm:ss" />td>
                <td>${item.detail }td>
                <td><a href="${pageContext.request.contentType }/item/editItem.action?id=${item.id}">修改a>td>
            tr>
            c:forEach>
        table>
    form>
  body>
html>

测试路径:http://127.0.0.1/springmvc_first/queryItem.action

测试了一下,乱码了,把jsp页面编码改一下就OK了

<%
response.setContentType("text/html;charset = GB2312");
request.setCharacterEncoding("gb2312");
%>

你可能感兴趣的:(springmvc,springmvc)