Struts2: type conversion-type encapsulation of Set set type attributes

Source: Internet
Author: User

This should be a topic of struts2 type conversion. This article mainly discusses how to batch encapsulate the values of forms on the page into a set, because Hibernate is used in the project, basically, all set types use the set type. For how to bulk encapsulate data to other collection types such as (list, map), please refer to this article for details: http://hi.baidu.com/wava/blog/item/01d58926c42dac158b82a134.html. How to assign a value to this set attribute takes quite some time. The following is an example.

Customerinserteraction. Java: process the request, containing a set type attribute orders

Package test. Web;

Import java. util. hashset;

Import java. util. List;

Import java. util. Set;

Import test. Persistence. customer;

Import test. Persistence. Order;

Import test. Service. icustomerservice;

Import com. opensymphony. xwork2.actionsupport;

Import com. opensymphony. xwork2.actioncontext;

 

Public class customerinserteraction extends actionsupport {

Private customer;

Private set <order> orders = new hashset ();

Private icustomerservice customerservice;

Public customer getcustomer (){

Return customer;

}

Public void setcustomer (customer ){

// Customer. setorders (orders );

This. Customer = customer;

}

Public String execute (){

Customer. setorders (orders );

For (order: orders)

Order. setcustomer (customer );

Customerservice. addcustomer (customer );

Return success;

}

Public set getorders (){

Return orders;

}

Public void setorders (set orders ){

This. Orders = orders;

}

Public icustomerservice getcustomerservice (){

Return customerservice;

}

Public void setcustomerservice (icustomerservice customerservice ){

This. customerservice = customerservice;

}

}

Customer. Java and order. Java: two entity classes, each representing a consumer and an order, and one consumption

Users can have multiple orders, and one order only belongs to one consumer.

Package test. persistence;

Import java. Io. serializable;

Import java. util. date;

Import java. util. hashset;

Import java. util. Set;

 

Public Class Customer implements serializable {

Private long ID;

Private string name;

Private string email;

Private string password;

Private string phone;

Private Boolean married;

Private string address;

Private char sex;

Private string description;

Private date birthday;

Private date registeredtime;

Private set orders = new hashset ();

Public customer (){

// Todo auto-generated constructor stub

}

Public customer (string name ){

Super ();

This. Name = Name;

}

Public customer (string name, set orders ){

Super ();

This. Name = Name;

This. Orders = orders;

}

Public String getaddress (){

Return address;

}

Public void setaddress (string address ){

This. Address = address;

}

Public String getdescription (){

Return description;

}

Public void setdescription (string description ){

This. Description = description;

}

Public date getbirthday (){

Return birthday;

}

Public void setbirthday (date birthday ){

This. Birthday = birthday;

}

Public String getemail (){

Return email;

}

Public void setemail (string email ){

This. Email = Email;

}

Public long GETID (){

Return ID;

}

Public void setid (long ID ){

This. ID = ID;

}

Public String getname (){

Return name;

}

Public void setname (string name ){

This. Name = Name;

}

Public String GetPassword (){

Return password;

}

Public void setpassword (string password ){

This. Password = password ;//

}

Public String getphone (){

Return phone;

}

Public void setphone (string phone ){

This. Phone = phone;

}

Public date getregisteredtime (){

Return registeredtime;

}

Public void setregisteredtime (date registeredtime ){

This. registeredtime = registeredtime;

}

Public char getsex (){

Return sex;

}

Public void setsex (char sex ){

This. Sex = sex;

}

Public set getorders (){

Return orders;

}

Public void setorders (set orders ){

This. Orders = orders;

}

Public Boolean ismarried (){

Return married;

}

Public void setmarried (Boolean married ){

This. Married = married;

}

}

Reg. jsp: front-end page for entering data

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" %>

<% @ Taglib prefix = "S" uri = "/Struts-tags" %>

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">

<HTML>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">

<Title> </title>

</Head>

<Body>

<Div style = "color: Red">

<S: fielderror/>

</Div>

<Form action = "register. Action" method = "Post">

<Table width = "389" border = "1" align = "center">

<Tr>

<TH Height = "30" colspan = "4" Scope = "col"> </Th>

</Tr>

<Tr>

<TD width = "65" Height = "30"> name </TD>

<TD width = "68"> <input name = "customer. Name" type = "text" id = "customer. Name" size = "10"> </TD>

<TD width = "75"> email </TD>

<TD width = "153"> <input name = "customer. Email" type = "text" id = "customer. Email" size = "10"> </TD>

</Tr>

<Tr>

<TD Height = "30"> address </TD>

<TD> <input name = "customer. Address" type = "text" id = "customer. Address" size = "10"> </TD>

<TD> phone number </TD>

<TD> <input name = "customer. Phone" type = "text" id = "customer. Phone" size = "10"> </TD>

</Tr>

<Tr>

<TD Height = "30"> password </TD>

<TD> <input name = "customer. Password" type = "text" id = "customer. Address" size = "10"> </TD>

<TD> gender </TD>

<TD> <input name = "customer. sex "type =" radio "id =" customer. sex "size =" 10 "value =" M "Checked =" checked "> male & nbsp; <input name =" customer. sex "type =" radio "id =" customer. sex "size =" 10 "value =" F "> female </TD>

</Tr>

<Tr>

<TD Height = "30"> Date of Birth </TD>

<TD> <input name = "customer. Birthday" type = "text" id = "customer. Address" size = "10"> </TD>

<TD> registration date </TD>

<TD> <input name = "customer. registertime" type = "text" id = "customer. Phone" size = "10"> </TD>

</Tr>

<Tr>

<TD Height = "30" colspan = "4"> Order 1 </TD>

</Tr>

<Tr>

<TD Height = "30"> Order No. </TD>

<TD> <input name = "orders. makenew [0]. ordernumber" type = "text" size = "10"> </TD>

<TD> delivery address </TD>

<TD> <input name = "orders. makenew [0]. Address" type = "text" size = "10"> </TD>

</Tr>

<Tr>

<TD Height = "30" colspan = "4"> order 2 </TD>

</Tr>

<Tr>

<TD Height = "30"> Order No. </TD>

<TD> <input name = "orders. makenew [1]. ordernumber" type = "text" size = "10"> </TD>

<TD> delivery address </TD>

<TD> <input name = "orders. makenew [1]. Address" type = "text" size = "10"> </TD>

</Tr>

<Tr>

<TD Height = "30" colspan = "4"> order 3 </TD>

</Tr>

<Tr>

<TD Height = "30"> Order No. </TD>

<TD> <input name = "orders. makenew [2]. ordernumber" type = "text" size = "10"> </TD>

<TD> delivery address </TD>

<TD> <input name = "orders. makenew [2]. Address" type = "text" size = "10"> </TD>

</Tr>

<Tr>

<TD Height = "30" colspan = "4"> <input type = "Submit" value = "Submit"> <input type = "reset" value = "reset"> </TD>

</Tr>

<Tr>

</Table>

</Form>

</Body>

</Html>

CustomerInserterAction-conversion.properties: type conversion configuration file, my contents are as follows

Keyproperty_orders = ID

Element_orders = test. Persistence. Order

Createifnull_orders = true

Note the following when encapsulating a set:

1. In customerinserteraction. Java, orders must be initialized as follows:

Private set <order> orders = new hashset ();

2. variables must be named in the following format on the JSP page:

<Input name = "orders. makenew [0]. ordernumber" type = "text" size = "10">

You must use the makenew operator so that ognl can create a new object for me. Otherwise, orders will be empty. If the set type is list at that time, you don't have to do this. You just need to directly name = "orders [0]. ordernumber.

3. Add keyproperty_orders = ID to the Class-conversion.properties file, which is required to encapsulate the set. This is mainly used to set the index for retrieving elements in a collection. For more information, see explain.

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.