<s:select>应用简介

来源:互联网 发布:微博情感分析数据集 编辑:程序博客网 时间:2024/06/06 00:23

一:前台对应的jsp页面

<%@ taglib prefix="s" uri="/META-INF/tld/struts/struts-tags.tld"%>
//这是引入S标签库struts-tags.tld,该标签库文件存放于上面的 uri中
<s:select name="sample">
 <option value="one">第一</option>
 <option value="two">第二</option>
 <option value="three">第三</option>
</s:select>


二:js对应的取值

如:选择下拉列表框中选中的“第二”
var temp1 = $('select[name="sample"]').find('option:selected').text();
var temp2 = s("select[name='sample']").find("option:selected").val();
则这里temp1的结果为“第二
则这里temp2的结果为“two

$('select[name="sample"]').html('');
作用:清空sample下拉列表框的下拉选项(内容)
$('select[name="sample"]').append($('<option>').val("four").html("第四");
作用:给sample下拉列表框添加一个选项,功能类似于:<option value="four">第四</option>

注意:这里的‘单双引号‘’和“”----》即双引号“”里面不能嵌套双引号“”,单引号‘’里面不能嵌套单引号‘’
js中标签中间的内容是  .text()------给标签添间添加加内容是  .html("添加的内容")


三:<s:select>标签的属性

headerValue :     下拉选项默认值value
headerKey    :     下拉选项默认name
listKey           :     下拉选项的name
listValue        :     下拉选项的value
list                 :      源数据 (可以在action方法里从数据库获取list)
name            :      被选中数据存放的位置(如:StudentVo.name),这里存储的内容为listkey的内容,下拉列表框中的选项显示为listvalue中存放的值
value             :      默认值(下拉列表框显示默认选中的值,或者初始化设定的值,这里需要设定的是listkey对应的内容)


EL表达式:  ${resultlist}  ${student.name}
OGNL表达式:#request.resultlist  #{student.name}
OGNL表达式依赖于struts2标签,必须结合struts2标签使用,且EL表达式不能用在struts2标签里

 

四:Java代码,jsp页面,数据表示例

1、数据表:

 

2、jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/META-INF/tld/struts/struts-tags.tld"%>
<%
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>select标签示例</title>   
 <script type="text/javascript" language="JavaScript" src="<%=basePath%>js/jquery/jquery-1.9.1.min.js"></script>
 <script>
  function getBack(){
   var url = "/jsp/business/huanying.jsp";
   location.href=url; //这里的location.href是在当前窗口中打开新的url网页
  } 
  function getStudent(){
   var studentId = $('#studentNo').val();
   var url = "<%=basePath%>allex/selectEx.action?studentVo.id=" + studentId;  //选中某一个选项后,进入修改页面可以默认显示该选中的值
   location.href = url;
  } 
 </script>
  </head>
  
  <body>
 <h2>请选择一个学生</h2>
 <form name="formEx">
  <s:select name="studentVo.id" id="studentNo" headerKey="" headerValue="--请选择--" theme="simple" list="#request.studentList" listKey="id" listValue="userName" value="#{studentVo.id}"onchange="getStudent()"/>
  <input type="button" value="返回" onclick="getBack();"/>
 </form>
  </body>
</html>

 

3、java代码:

package cn.test.allex.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import cn.test.base.SystemInstance;
import cn.test.business.service.Iimpl.IStudentService;
import cn.test.business.vo.StudentVo;

public class AllexAction {
 private StudentVo studentVo = null;
 private IStudentService studentService = (IStudentService)SystemInstance.getInstance().getBean(IStudentService.class);
 
 public StudentVo getStudentVo() {
  return studentVo;
 }
 public void setStudentVo(StudentVo studentVo) {
  this.studentVo = studentVo;
 }
 
 /* select下拉列表框中内容初始化 */
 public String preSelectEx(){
  boolean result = false;

  List<StudentVo> voList = new ArrayList<StudentVo>();
  if(null == studentVo){
   studentVo = new StudentVo();
   studentVo.setId("402881924bc4b3c4014bc4f74c340001");  //jsp页面下拉框默认值的显示:该"402881924bc4b3c4014bc4f74c340001"是world学生的id
  }
  result = studentService.getVoList(voList);  //获得数据表中的内容--获得学生列表DAO操作略
  if(result){
   ServletActionContext.getRequest().setAttribute("studentList", voList);
   return "preSelectEx_success";
  }else{
   return "preSelectEx_fail";
  }
 }
}

 

5、运行结果:

(1)显示初始设定的world默认选中(该select下拉框在headvalue和value同时存在时,显示value设定的默认值)

(2)显示下拉框的内容

 

 

1 0
原创粉丝点击