在Struts2中,给我们提供了一个s:doubleselect标签,该标签可以实现级联下拉选择。
doubleselect.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>Document</title>
<%@ taglib prefix="s" uri="/struts-tags" %>
<style type="text/css">select{width:200px;height:40px;}</style>
</head>
<body>
<s:form action="resultAction" namespace="/">
<s:bean name="top.k10000.DoubleSelectAction" id="location"></s:bean>
<s:doubleselect
label="Server (OGNL) "
name="province"
list="#location.map.keySet()"
doubleName="city"
doubleList="#location.map[top]" />
<s:submit value="submit" name="submit" />
</s:form>
</body>
</html>
result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta charset="utf-8">
<title>Document</title>
</head>
<body>
<s:property value="province"/> , <s:property value="city"/>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="doubleSelectAction" class="top.k10000.DoubleSelectAction" method="display">
<result name="success">/doubleselect.jsp</result>
</action>
<action name="resultAction" class="top.k10000.DoubleSelectAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>doubleselect.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>
DoubleSelectAction.java
package top.k10000;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
public class DoubleSelectAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String province;
private String city;
Map<String, ArrayList<String>> map;
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;
}
public Map<String, ArrayList<String>> getMap() {
return map;
}
public void setMap(Map<String, ArrayList<String>> map) {
this.map = map;
}
public DoubleSelectAction(){
map =new HashMap<String, ArrayList<String>>();
map.put("广东省", new ArrayList<String>(Arrays.asList("广州市", "深圳市", "汕尾市","东莞市")));
map.put("北京", new ArrayList<String>(Arrays.asList("东城区", "西城区","朝阳区")));
map.put("新疆省", new ArrayList<String>(Arrays.asList("乌鲁木齐","克拉玛依市","图木舒克市")));
System.out.println(map);
}
public String execute() {
return SUCCESS;
}
public String display() {
return SUCCESS;
}
}
园子参考:https://www.cnblogs.com/IT-1994/p/6135952.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/151217.html