springMVC(11)------数据列表显示

来源:互联网 发布:淘宝的延长收货是几天 编辑:程序博客网 时间:2024/04/30 05:55

页面数据列表显示,配合jstl标签。

一,项目结构


二,所需jar包

commons-logging.jar

spring-aop-4.1.8.RELEASE.jar

spring-beans-4.1.8.RELEASE.jar

spring-context-4.1.8.RELEASE.jar

spring-core-4.1.8.RELEASE.jar

spring-expression-4.1.8.RELEASE.jar

spring-web-4.1.8.RELEASE.jar

spring-webmvc-4.1.8.RELEASE.jar


以下两个为jstl标签库所需

standard.jar

jstl.jar


三,web.xml配置spring MVC的DispatcherServlet

<?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">    <!-- 配置DispatcherServlet -->  <servlet>  <servlet-name>springMVCDispatcherServlet</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <!-- 配置DispatcherServlet的初始化参数 -->  <init-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:spring-mvc.xml</param-value>  </init-param>  <!-- web应用被加载的时候创建servlet -->  <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>  <servlet-name>springMVCDispatcherServlet</servlet-name>  <url-pattern>/</url-pattern>  </servlet-mapping>    <welcome-file-list>      <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>

四,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:mvc="http://www.springframework.org/schema/mvc"         xmlns:p="http://www.springframework.org/schema/p"         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-4.1.xsd              http://www.springframework.org/schema/context               http://www.springframework.org/schema/context/spring-context-4.1.xsd              http://www.springframework.org/schema/aop               http://www.springframework.org/schema/aop/spring-aop-4.1.xsd              http://www.springframework.org/schema/tx               http://www.springframework.org/schema/tx/spring-tx-4.1.xsd              http://www.springframework.org/schema/mvc               http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd              http://www.springframework.org/schema/context               http://www.springframework.org/schema/context/spring-context-4.1.xsd">  <!--  配置自动扫描的包,扫描到@controller注解的类是控制器,是使用注解的前提  --><context:component-scan base-package="com.lanhuigu.springmvc"/><!-- 视图解析器 --><bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <!-- 配置前缀 -->  <property name="prefix" value="/WEB-INF/views/"/>  <!-- 配置后缀 -->  <property name="suffix" value=".jsp"/></bean><!-- 自定义视图解析器:BeanNameViewResolver使用视图的名字解析视图 -->      <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">          <!--           设置该自定义视图解析器BeanNameViewResolver与            InternalResourceViewResolver视图解析器的优先级值,           InternalResourceViewResolver的order默认值为Integer的最大值,           BeanNameViewResolver以下设置order为100,order值越小, 视图解析器的优先级越高。             该配置文件中自定视图解析器BeanNameViewResolver的优先级高于           InternalResourceViewResolver,每次先使用BeanNameViewResolver解析视图,           如果实现不了需求, 自动调用InternalResourceViewResolver解析视图。          -->          <property name="order">              <value>100</value>          </property>      </bean><!-- 配置直接转发的页面 --><mvc:view-controller path="/success" view-name="success"/><!-- 解决mvc:view-controller配置后RequestMapping映射地址报404的问题 -->      <mvc:annotation-driven></mvc:annotation-driven></beans>

五,实体类User和Address

User.java

package com.lanhuigu.springmvc.entities;public class User {private String username;private String password;private Address address;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}}
==================================================================

Address.java

package com.lanhuigu.springmvc.entities;public class Address {private String province;private String city;public String getProvince() {return province;}public void setProvince(String province) {this.province = province;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}}

六,Restful风格TestRestfulController.java

package com.lanhuigu.springmvc.controller;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.lanhuigu.springmvc.entities.Address;import com.lanhuigu.springmvc.entities.User;@Controller@RequestMapping("/testRestful")public class TestRestfulController {@RequestMapping("/list")public String queryList(Map<String,Object> map) {List<User> lists = new ArrayList<User>();// 这里没有建立持久层与数据库交互,硬编码两条数据做测试User user1 = new User();          user1.setUsername("ONE");          user1.setPassword("111111");          Address address1 = new Address();          address1.setProvince("aaaaaa");          address1.setCity("bbbbbb");                user1.setAddress(address1);          //=========================        User user2 = new User();          user2.setUsername("TWO");          user2.setPassword("222222");          Address address2 = new Address();          address2.setProvince("cccccc");          address2.setCity("dddddd");                user2.setAddress(address2);          // User对象添加到List中        lists.add(user1);        lists.add(user2);        // 将List封装到Map中        map.put("users", lists);        return "list";}}

六,list.jsp页面,该页面位于WEB-INF下的views下

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  <%        String path = request.getContextPath();        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";     %>     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">     <html>        <head>          <title>spring MVC实例初体验</title>        </head>                <body>            <table border="1" cellpadding="10" cellspacing="0">              <tr>                  <th>用户名</th>                  <th>密码</th>                  <th>省份</th>                  <th>城市</th>              </tr>                            <c:forEach items="${users }" var="user">                  <tr>                      <td>${user.username }</td>                      <td>${user.password }</td>                      <td>${user.address.province }</td>                      <td>${user.address.city }</td>                  </tr>              </c:forEach>          </table>      </body>    </html>

<c:forEach items="${users} var="user" ></c:forEach>为java标准标签库,即jstl库中功能,

用于循环list,将数据展现在页面。

七,访问地址

http://localhost:9000/SpringMVC/testRestful/list

八,页面结果


0 0
原创粉丝点击