struts2标签之select标签

1 struts2中的select标签为下拉列表标签。该标签的list属性可以写死也可以从后台得到。
如:<s:select name="bean.sexSer" list="#{0:'男',1:'女'}" listKey="key" listValue="value" headerKey="-1" headerValue="==请选择1==" value="#request.bean.sexSer"/>


解释:
name属性为标签的name值,
list属性为该标签的列表。上述是写死:list="#{0:'男',1:'女'}",key为整形,value为字符型
listKey属性为该list属性中的key
listValue属性为该list属性中的value
headerKey属性为默认的key
headerValue属性为默认的value
value属性为选择后,选中的值显示出来。


value中的写法:#request.bean.sexSer 使用struts2中的ongl表达式的写法。一般是:#request.action的属性(例如是bean).bean的属性


2 struts2中的select标签中的list属性可以从后台得到。如:
<s:select name="bean.sexSer" list="#request.bean.list" listKey="sexKey" listValue="sexValue" headerKey="-1" headerValue="==清==" value="#request.bean.sexSer"/>


解释:list属性为通过ongl表达式得到的list值。
listKey属性为list中的key
listValue属性为list中的value
headerKey属性为默认的key
headerValue属性为默认的value
value属性为选择后,选中的值显示出来。


后台的list的写法:
首先在对应的bean中添加list属性,list属性的对象必须是自定义好的类。同时设置好get和set方法
如:private List<SexBean> list=new ArrayList<SexBean>();


然后在action中添加bean属性,并为其设置get和set方法。


然后在action中在调用方法前进行初始化的工作。为bean的list赋值。


最后在页面上通过#request.bean.list来展现出来。


3简单的说说。通过ongl表达式在页面显示出来的值的方法:


a:在普通的html标签中可以通过EL表达式来显示:
如:<input type="text" value="${bean.sexSer}"/>


b:在struts2的标签中可以使用ongl表达式:
如:a:<s:property value="#request.bean.sexSer"/>
  b:<s:property value="%{bean.sexSer}"/>
  c:<s:property value="bean.sexSer"/>
  d:<s:property value="%{#request.bean.sexSer}"/>


有以上的4中写法:
第一种:#request.action的属性(例如bean).bean的属性 如:#request.bean.sexSer
第二种:%{action的属性(例如bean).bean的属性}   如:%{bean.sexSer}
第三种:action的属性(例如bean).bean的属性           如:bean.sexSer
第四种:%{#request.action的属性(例如bean).bean的属性} 如:%{#request.bean.sexSer}


解释:%{}表示括号内的为ongl表达式
      #表示使用action的上下文:#表示ServletActionContext.getContext()


完整的例子:
//SexBean代码
public class SexBean {


private int sexKey;
private String sexValue;
public SexBean() {
super();
}

public SexBean(int sexKey, String sexValue) {
super();
this.sexKey = sexKey;
this.sexValue = sexValue;
}


public int getSexKey() {
return sexKey;
}
public void setSexKey(int sexKey) {
this.sexKey = sexKey;
}
public String getSexValue() {
return sexValue;
}
public void setSexValue(String sexValue) {
this.sexValue = sexValue;
}

}


//Example 代码
public class Example {


private int id;
private String name;
private String sex;
private String remark;

private java.util.Date createDate;

private String createDateSer;
private String sexSer;

private List<SexBean> list=new ArrayList<SexBean>();

private List<Example> resultLists;
public Example() {
super();

}

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;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public List<Example> getResultLists() {
return resultLists;
}
public void setResultLists(List<Example> resultLists) {
this.resultLists = resultLists;
}
public java.util.Date getCreateDate() {
return createDate;
}
public void setCreateDate(java.util.Date createDate) {
this.createDate = createDate;
}
public String getCreateDateSer() {
return createDateSer;
}
public void setCreateDateSer(String createDateSer) {
this.createDateSer = createDateSer;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSexSer() {
return sexSer;
}
public void setSexSer(String sexSer) {
this.sexSer = sexSer;
}


public List<SexBean> getList() {
return list;
}


public void setList(List<SexBean> list) {
this.list = list;
}

}


//ExampleAction 代码
public class ExampleAction extends ActionSupport{


private Example bean;

private ExampleService exampleService;

public ExampleAction() {
super();

}
public void init(){
System.out.println("action初始化....");
SexBean sex0=new SexBean(0,"男");
SexBean sex1=new SexBean(1,"女");
List<SexBean> sexList=new ArrayList<SexBean>();
sexList.add(sex0);
sexList.add(sex1);

bean.setList(sexList);

}


public Example getBean() {
return bean;
}


public void setBean(Example bean) {
this.bean = bean;
}


public ExampleService getExampleService() {
return exampleService;
}


public void setExampleService(ExampleService exampleService) {
this.exampleService = exampleService;
}

public String query(){
init();
System.out.println("query");
List<Example> lists=this.exampleService.query(bean);
System.out.println("size:"+lists.size());
if(bean!=null){
bean.setResultLists(lists);
}else{
System.out.println("bean is null");
}
return "success";
}

public String queryMethod(){
init();
System.out.println("queryMethod");
List<Example> lists=this.exampleService.query(bean);
System.out.println("size:"+lists.size());
if(bean!=null){
bean.setResultLists(lists);
}else{
System.out.println("bean is null");
}
return "success";
}


public String list(){
init();
System.out.println("list");
String sexSer=bean.getSexSer();
if(sexSer.equals("-1")){
bean.setSexSer(null);
}
List<Example> lists=this.exampleService.query(bean);
System.out.println("size:"+lists.size());
if(bean!=null){
bean.setResultLists(lists);
}else{
System.out.println("bean is null");
}
return "success";
}



}


//jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>
    <base href="<%=basePath%>">
    
    <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">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" language="javascript" src="<%=basePath %>common/WDatePicker/WdatePicker.js"></script>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
.table_css{
border:#666666 solid 1px;
border-color:#666666 solid 1px;
}
.table_css tr{
border:#666666 solid 1px;
border-color:#666666 solid 1px;
}

.table_css tr td{
border:#666666 solid 1px;
border-color:#666666 solid 1px;
}
</style>
  </head>
  <script language="javascript">
function btn_btn1_onclick(){
document.form1.action="userAddAction!add";
document.form1.submit();
}
  </script>
  <body>
  <form name="form1" action="exampleQuery!list" method="post">
  <%--<table>
  <tr>
  <td>姓名</td>
  </tr>
  </table>
  --%><%--<s:select name="bean.sexSer" list="#{0:'男',1:'女'}" listKey="key" listValue="value" headerKey="-1" headerValue="==请选择1==" value="#request.bean.sexSer"/>--%>

<s:select name="bean.sexSer" list="#request.bean.list" listKey="sexKey" listValue="sexValue" headerKey="-1" headerValue="==清==" value="#request.bean.sexSer"/>
<input type="text" id="bean.creatDateSer" name="bean.createDateSer" class="Wdate" value="${bean.createDateSer}" οnfοcus="WdatePicker({isShowWeek:true,dateFmt:'yyyy-MM-dd'})"/>
  <input type="button" name="btn_query" id="btn_query" value="查询" οnclick="btn_query_onclick()"/>
  <input type="text" value="${bean.sexSer}"/>
 
  a:<s:property value="#request.bean.sexSer"/>
  b:<s:property value="%{bean.sexSer}"/>
  c:<s:property value="bean.sexSer"/>
  d:<s:property value="#request.bean.sexSer"/>
  e:<s:property value="%{#request.bean.sexSer}"/>
  </form>
  <table class="table_css">
  <tr>
  <td>id</td>
  <td>name</td>
  <td>sex</td>
  <td>remark</td>
  </tr>
  <s:if test="#request.bean.resultLists!=null&&#request.bean.resultLists.size()>0">
 
  <s:iterator id="exampleLists" value="#request.bean.resultLists" status="">
  <tr>
  <td>
  <s:property value="#exampleLists.id"/>
  </td>
  <td>
  <s:property value="#exampleLists.name"/>
  </td>
  <td>
  <s:if test="#exampleLists.sex==0">
 
  </s:if>
  <s:if test="#exampleLists.sex==1">
 
  </s:if>
  </td>
  <td>
  <s:property value="#exampleLists.remark"/>
  </td>
  </tr>
  </s:iterator>
  </s:if>
  </table>
  </body>
  <script type="text/javascript" language="javascript">
function btn_query_onclick(){
//alert("111");

var sex=document.getElementById("bean.sexSer").value;
//alert(sex);


document.form1.action="exampleQuery!list";
document.form1.submit();
}
  </script>
</html>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值