java--jsp+ssh+select動態綁定資料並選中(解決方案),java--jspssh
在三層架構與jsp組合的項目中,如何?select動態綁定資料並動態選中指定行?且看下文:
1、先定義一個Bean類,用於執行個體化select綁定的每一條資料的id和name:
public class DropDownListBean { private String id; private String name; public String getId() {return id; } public void setId(String id) {this.id = id; } public String getName() {return name; } public void setName(String name) {this.name = name; } }
2、在service商務邏輯層中,要有dropdownlist資料來源list的get和set方法:
public class ToDepartmentUpdatePageAction extends ActionSupport {/** * */private static final long serialVersionUID = 1L;private DepartmentBean departmentBean;private List<DropDownListBean> list = new ArrayList<DropDownListBean>();private int id;public int getId() {return id;}public void setId(int id) {this.id = id;}@Resource private IDepartmentManage departmentManage;public DepartmentBean getDepartmentBean() {return departmentBean;}public void setDepartmentBean(DepartmentBean departmentBean) {this.departmentBean = departmentBean;}public IDepartmentManage getDepartmentManage() {return departmentManage;}public void setDepartmentManage(IDepartmentManage departmentManage) {this.departmentManage = departmentManage;}public List<DropDownListBean> getList() {return list;}public void setList(List<DropDownListBean> list) {this.list = list;}@Overridepublic String execute() throws Exception {setDepartmentBean(departmentManage.getDepartments0(" where nid = "+id).get(0));List tmpList=departmentManage.getddlDepartments();for (int i = 0; i < tmpList.size(); i++) {Object[] objects = (Object[]) tmpList.get(i);DropDownListBean dropDownListBean = new DropDownListBean();dropDownListBean.setId((String)objects[0]);dropDownListBean.setName((String)objects[1]);list.add(dropDownListBean);}return "success";}}
3、在jsp頁面中擷取list資料來源以及bean對象:
<% DepartmentBean departmentBean = (DepartmentBean)request.getAttribute("departmentBean"); String selectParentDeptId = String.valueOf(departmentBean.getParentNo()); List<DropDownListBean> ddlList=(List<DropDownListBean>)request.getAttribute("list");%>
4、在jsp頁面中給select動態綁定資料並動態選中:
<select name="departmentBean.parentNo" id="parentNo"> <% for(int i=0;i<ddlList.size();i++){ DropDownListBean dropDownListBean=ddlList.get(i); %><option value="<%=dropDownListBean.getId()%>" <%if(selectParentDeptId.equals(dropDownListBean.getId().toString())){out.print("selected");} %> ><%=dropDownListBean.getName()%></option> <% } %> </select>
用SSH架構開發web,頁面有一個select標籤(值從資料庫中綁定),怎選擇select的值時觸發事件調用 Action方
調用Action 中的方法 ???
不知道。 你是想說, 當選擇一個選項的時候, 跳轉到 Action 中的要求方法呢。
還是 用ajax 向Action 中的方法發出請求呢。
還是僅僅 和 DWR 一樣,調用這個方法?
如果是第一種。很簡單啊。
<select onchange = "goto(this)">
</select>
<script type = "text/javascript">
function goto(obj)
{
var opt = obj.value; //獲得選擇的值
//可以根據選擇的值給action 傳入參數,或者,跳入不同action
location.href = "xxx.action"; //請求 目標action
//這是最簡單的請求 action的方法。 如果需要ajax的,更簡單。 只要 把location.href替換城ajax請求就可以了。
}
</script>
使用SSH架構將資料庫表的某個欄位值動態綁定到頁面select標籤
只要action欄位屬性中有相應欄位的getset方法 便能自動綁定到strus的標籤