Java -- jsp + ssh + select Dynamic Data Binding and selection (solution), java -- jsph

Source: Internet
Author: User

Java -- jsp + ssh + select Dynamic Data Binding and selection (solution), java -- jsph

In a three-tier architecture and jsp combination project, how does one implement select Dynamic Data Binding and dynamic selection of specified rows? See the following:

1. Define a Bean class to instantiate the id and name of each data entry bound to the select statement:

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. In the service business logic layer, the get and set methods of the dropdownlist data source list must be available:

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. Obtain the list data source and bean object on the jsp page:

<% DepartmentBean departmentBean = (DepartmentBean)request.getAttribute("departmentBean"); String selectParentDeptId = String.valueOf(departmentBean.getParentNo());  List<DropDownListBean> ddlList=(List<DropDownListBean>)request.getAttribute("list");%>

4. dynamically bind data to select on the jsp page and dynamically select the data:

      <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>



Develop the web Using SSH framework. The page has a select tag (the value is bound from the database). How to select the select value to trigger the event to call the Action Party?

Call the method ???
I don't know. If you select an option, you can jump to the Request Method in Action.

Or use ajax to send a request to the Action method.

Or is this method called just like DWR?

For example. It's easy.

<Select onchange = "goto (this)">
</Select>

<Script type = "text/javascript">
Function goto (obj)
{
Var opt = obj. value; // obtain the selected value
// You can input parameters to the action based on the selected value, or jump to different actions.
Location. href = "xxx. action"; // request target action

// This is the simplest way to request action. It is simpler to use ajax. You only need to replace location. href with the ajax request.
}
</Script>

Use the SSH framework to dynamically bind a field value of a database table to the select tag on the page.

As long as the getset method of the corresponding field in the action field property can be automatically bound to the strus tag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.