一、前言
这段时间忙的要死,做项目,学框架,时间根本不够用,只能尽量抽出时间记录自己学过的东西。
1.1、doubleSelect
在之前学习中,我们使用过二级列表,相信很多人都理解其原理,在struts中,同样也为我们准备了二级列表,使用doubleSelect就能够时间
进入主题
1、搭建环境(这里笔者使用的struts框架是2.3的,传送门)
2、配置struts2.3环境
2.1、web.xml配置过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>LearStruts2_4</display-name> <welcome-file-list> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2.2、在WEB-INF中新建class文件夹,文件夹下新建struts.xml配置信息
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="doubleselectTag" class="com.struts.ui.action.DoubleSelectTagAction"> <result name="success">/doubleSelectTag.jsp</result> </action> </package> </struts>
3、新建实体类
package com.struts.ui.action; public class City { private int id; private String name; public City(){ } public City(int id,String name){ this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.struts.ui.action; public class Provice { private int id; private String name; public Provice(){ } public Provice(int id,String name){ this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4、新建DoubleSelectTagAction类继承ActionSupport
package com.struts.ui.action; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.ValueStack; /** * 使用doubleSelect 二级联下拉,配置环境是struts2.3 tomcat8.0 * 相关的类有: * City.java * Provice.java * doubleSelectTag.jsp * DoubleSelectTagAction.java * 访问:http://localhost:8080/LearStruts2_4/doubleselectTag * 目的:简单就是选择一级列表之后出现二级菜单选项 * 这里使用的是传递对象的id到jsp页面,之后通过id查找 * */ public class DoubleSelectTagAction extends ActionSupport { private List<Provice> provices;// Provice对象的列表 private Map<Integer, List<City>> cityMap;// 以Provice对象为key,Provice对应的City对象的列表作为value public Map<Integer, List<City>> getCityMap() { return cityMap; } public DoubleSelectTagAction(){ provices = new ArrayList<Provice>(); Provice provice1 = new Provice(1,"四川省"); Provice provice2 = new Provice(2,"山东省"); Provice provice3 = new Provice(3,"湖南省"); Provice provice4 = new Provice(4,"广东省"); provices.add(provice1); provices.add(provice2); provices.add(provice3); provices.add(provice4); List<City> cities1 = new ArrayList<City>(); List<City> cities2 = new ArrayList<City>(); List<City> cities3 = new ArrayList<City>(); List<City> cities4 = new ArrayList<City>(); cities1.add(new City(1,"成都市")); cities1.add(new City(2,"绵阳市")); cities2.add(new City(1,"济南市")); cities2.add(new City(2,"青岛市")); cities3.add(new City(1,"长沙市")); cities3.add(new City(2,"郴州市")); cities4.add(new City(1,"广州市")); cities4.add(new City(2,"深圳市")); cityMap = new HashMap<Integer,List<City>>(); cityMap.put(provice1.getId(), cities1); cityMap.put(provice2.getId(), cities2); cityMap.put(provice3.getId(), cities3); cityMap.put(provice4.getId(), cities4); } public String execute() throws Exception { return SUCCESS; } public List<Provice> getProvices(){ return provices; } }
5、新建jsp视图页面显示数据
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!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> <s:form action="doubleselectTag"> <s:doubleselect label="请选择所在省市" name="provice" list="provices" listKey="id" listValue="name" doubleList="cityMap.get(top.id)" doubleListKey="id" doubleListValue="name" doubleName="city" headerValue="----请选择----" emptyOption="true" /> </s:form> <!-- 需要注意的点: 1、list 是doubleSelectTagAction中第一个泛型集合的名称 2、listKey 是City类的属性id 3、listValue 是City类的属性name 4、doubleList 是读取map集合,top是当前list中集合的对象,注意 cityMap.get(top.id)中的id和listkey的值必须相同,cityMap是集合名称 5、doublelistkey 是map集合对象的id 如果DoubleSelectTagAction中 map集合存放的键是对象的话,那么doubleList就必须改为对象【doubleList="cities"】 --> </body> </html>
注意点:此处是map集合的键是id,所以使用到cityMap.get(top.id),下面这种是map集合为对象的时候,代码只是有点不同。
DoubleSelectTagAction
jsp页面
doubleSelectTag.jsp
效果如下:效果是有的,是笔者的录制软件的问题
总结:
原理跟二级列表类似,需要注意的点是:当使用id时,必须保证listkey=id 和 doubleList= citymap.get(top.id) 中的top.id相同。代码亲测,可以使用。
笔者自己看的博客