Struts tag details

Source: Internet
Author: User
The development of action and JSP is actually the application of struts labels. the proficiency of tags determines the development efficiency. beginners often have no choice but to express or obtain certain data. A simple problem is a waste of one or two days. this delay the entire development process. the struts books on the outside show that the label and data transmission principles are relatively simple. Next I will analyze the label technology and data transmission principles in a comprehensive and multi-angle manner. hope to help you. this template will greatly improve the development efficiency. use sample as the function name.
① There is a text box on the screen, showing the content as a certain field in a data table. How should we set and get the data?
Samplejsp:
<HTML: Text name = "sampleform" property = "name"/>
Sampleform. Java: // form the file name must correspond to the name of the tag in JSP
String name; // it must be the same as the property of the project in JSP.
Public String getname () {return name ;}
Public void setname (string name) {This. Name = Name ;}
Variable and method name. It is not acceptable. Variable ABCD, that method name is setabcd and getabcd. Pay attention to Case sensitivity.
The items in JSP must all be expressed in form. Of course, the items in form may not all be expressed in JSP (objects or verification of auxiliary actions may exist)
Sampleaction. Java
Public actionforward start (actionmapping mapping,
Actionform argform, httpservletrequest req, httpservletresponse res)
Throws exception {
Sampleform form = (sampleform) argform;
String name = .................. Other codes for get name from DB
// Set Name
Form. setname (name );
// Now text will show the name
}
Public actionforward save (actionmapping mapping,
Actionform argform, httpservletrequest req, httpservletresponse res)
Throws exception {
Sampleform form = (sampleform) argform;
// Get name
String name = form. getname ();
.................. Other codes for save name
}
JSP corresponds to form. Action Operations form and form actually play a role in data transmission. this is the core principle of struts labels. it's okay to get the data and set the data, and the rest of the work will be handy.

② Let's look at a method for processing tags. The screen shows a detailed list (table). It indicates the data (ID, name) related to the user of the data table ).
Samplejsp:
<Logic: present name = "sampleform" property = "userlist">
<Logic: iterate id = "user" name = "sampleform" property = "userlist">
<Tr>
<TD> <Bean: write name = "user" property = "ID"/> </TD>
<TD> <Bean: write name = "user" property = "name"/> </TD>
</Tr>
</Logic: iterate>
</Logic: Present>

Logic: present is a logical judgment. userlist in sampleform is empty (no data or null), and the following items are not displayed.
Logic: iterate is a logical loop. If userlist contains several pieces of data, it loops several times.

<Bean: write name = "user" property = "ID"/> is a lable label that displays the ID attribute of the user object (entity. or display the ID column of a record in the User table.
User. Java (it is entity, because it is close to the business, as high as no development, please remember not to modify it. In case of design problems, QA Japan)
String ID;
Public String GETID () {return ID ;}
Public void setid (string ID) {This. ID = ID ;}
String name;
Public String getname () {return name ;}
Public void setname (string name) {This. Name = Name ;}
Do you think you are familiar with this? It seems to be the same as form, but it is a bit different. You can feel it after reading it.
Sampleform. Java:
List userlist;
Public list getuserlist () {return userlist ;}
Public void setuserlist (list userlist) {This. userlist = userlist ;}
As long as the form is used, you will ask, how can we get the ID, name, and struts? Didn't you say that JSP must correspond to form in the same way? Yes. The one-to-one correspondence is positive. The userlist information already contains everything. Do you need to define the ID and name? As for how struts obtains data, let's take a look at how the following actions are handled.
Sampleaction. Java
Public actionforward start (actionmapping mapping,
Actionform argform, httpservletrequest req, httpservletresponse res)
Throws exception {
Sampleform form = (sampleform) argform;
Arraylist userlist = new arraylist ();
User user = new user ();
User. setid (1 );
User. setname ("name1 ");
Userlist. Add (User );

User user = new user ();
User. setid (2 );
User. setname ("name2 ");
Userlist. Add (User );

// Set userlist
Form. setuserlist (userlist );
// Now table will show
}
Everything is done. Isn't it easy, but it's estimated that you are still a little dizzy. You still want to ask me, how do I set the ID and name?
Setting userlist for action is enough. It contains enough information. struts saw that you set userlist. it will know that there are not many get and set methods in the list of all users (entity) and useruser (entity?

Let's look at the following stuff.
<Logic: iterate id = "user" name = "sampleform" property = "userlist">
<Bean: write name = "user" property = "ID"/>
Id = "user", corresponding to name = "user". What do you mean ?. Just like specifying the index in a loop. Property = "ID" is the content corresponding to the index to be displayed. Struts recognizes the ID and name in this way.

③ Next, let's look at an enhanced table example. Add a radio box in front of each row in the detailed list, so that you can select which user to delete.
Samplejsp:
<Logic: present name = "sampleform" property = "userlist">
<Logic: iterate id = "user" name = "sampleform" property = "userlist">
<Tr>
<TD>
<HTML: Radio name = "sampleform" property = "selecteduserid" value = "/<% = (JP. co. mhcb. obs. persis. entity. user) pagecontext. getattribute ("user ")). GETID (). tostring () %> "/>
</TD>
<TD> <Bean: write name = "user" property = "ID"/> </TD>
<TD> <Bean: write name = "user" property = "name"/> </TD>
</Tr>
</Logic: iterate>
</Logic: Present>

Sampleform. Java:
String selecteduserid;
Public String getselecteduserid () {return selecteduserid ;}
Public void setselecteduserid (string selecteduserid ){
This. selecteduserid = selecteduserid;
}
Sampleaction. Java
Public actionforward Delete (actionmapping mapping,
Actionform argform, httpservletrequest req, httpservletresponse res)
Throws exception {
Sampleform form = (sampleform) argform;
String selecteduserid = form. getselecteduserid ();
// Get user by selected ID
User user = getuser (selecteduserid );
// Delete user
}
Radio box. the propertys value corresponds to the object in the form. the value of value is the ID of the user corresponding to the row Radio (the user ID in the data table is the primary key). When the user selects any radio, Struts obtains the propertys value through form, you can obtain the selected user and perform the corresponding operations.
Select the user to be selected. 2. Use program control. If the initial screen is displayed. the radio id = '3' is selected, as long as the form is in the initial action. selecteduserid ("3"); everything is done, just one sentence, when entering the initial screen, user. the radio id = '3' is selected.

Note the following labels
<HTML: Radio name = "sampleform" property = "selecteduserid" value = "<% = (JP. co. mhcb. obs. persis. entity. user) pagecontext. getattribute ("user ")). GETID (). tostring () %> "/>
What do the following labels mean?
<HTML: Radio name = "sampleform" property = "selecteduserid" value = "<% = (JP. co. mhcb. obs. persis. entity. user) pagecontext. getattribute ("user ")). getobject1 (). getobject1 (). getobject2 ()............ Getobjectn (). GETID (). tostring () %> "/>
What can we see?
User contains object1, object2 contains object3 ,.... The objectN-1 contains objectn and objectn has the ID attribute.
See it? Flexible use. Imagine how to write each entity, form, and action?

④ Next we will introduce how to set and obtain data when using checkbox. There is a row of checkbox in the screen? Let's take a look at a simple one.
<HTML: checkbox name = "sampleform" property = "chechbox1" value = "true"/>
<HTML: checkbox name = "sampleform" property = "chechbox2" value = "false"/>
<HTML: checkbox name = "sampleform" property = "chechbox3" value = "true"/>
The second box is not selected. The other selected. Form faces three strings chechbox1, chechbox2, and chechbox3. The following is a complex dialog box multibox.
Samplejsp:
<Logic: iterate name = "sampleform" id = "user" property = "userlist">
<HTML: multibox property = "selectedusers">
<Bean: write name = "user" property = "ID"/>
</Html: multibox>
<Bean: write name = "user" property = "name"/>
</Logic: iterate>

Sampleform:
Private string userlist [] = new string [0];
Public String [] getuserlist () {return userlist ;}
Public void setuserlist (string [] userlist) {This. userlist = userlist ;}

Private string selectedusers [] = new string [0];
Public String [] getselectedusers () {return selectedusers ;}
Public void setselectedusers (string [] selectedusers) {This. selectedusers = selectedusers ;}

If we assign values to bean in action at the initial stage:
Userlist = {user ("1", "name1"), user ("2", "name2"), user ("3", "name3 ")}
Selectedusers = {"1", "3 "}
Select the first and third selection boxes.

The user selects the second and third option in the modify selection box. Then, the bean value is obtained in the action.
String selecteditems [] = new string [list. getsize ()];
Selecteditems = form. getselecteditems ();
For (INT I = 0; I <selecteditems. length; ++ I ){
Logger. debug ("selected" + I + ":" + selecteditems [I]);
}
Selected 0: 2
Selected 1: 3
Selectedusers = {"2", "3 "}

⑤ There is a user table on the screen, with a button in front of each piece of data, corresponding to a record. How can I determine the selected data ??
Samplejsp:
<Logic: iterate id = "user" indexid = "buttonindex" name = "sampleform" property = "userlist">
<Tr>
<TD>
<HTML: Submit property = "button" indexed = 'false'>
<Bean: Message key = "label. Button. selectuser"/>
</TD>
<TD> <Bean: write name = "user" property = "ID"/> </TD>
<TD> <Bean: write name = "user" property = "name"/> </TD>
</Tr>
<HTML: hidden name = "sampleform" property = "selectuserindex" value = '<% = "" + buttonindex %>'/>
</Logic: iterate>

Sampleaction. Java
Int Index = integer. parseint (Form. getselectuserindex ());
Use a Hidden variable to obtain the selected data, and then perform corresponding processing.

6. The above data is transmitted through form and JSP. the session can also display data in JSP. however, as a designer, I do not advocate this. why not. however, the previous design in Japan may use session and JSP to transmit data. then I need to explain how to use it? As a high-speed designer, do not use session to communicate with JSP.
There is a drop-down list box showing all user names. Use session to transmit data.
Samplejsp:
<% Pagecontext. setattribute ("userlist", (list) (fwthreadcontext
. Getattribute ("alluser ")));
%>
<HTML: Select Property = "selecteduser">
<HTML: Options collection = "userlist" property = "ID" labelproperty = "name"/>
</Html: Select>

Sampleform. Java:
String selecteduser;
Only one selecteduser in form indicates the selected user. The drop-down list box is represented by session.
If the session content is set in action and other places, the drop-down list box will show the content. here, the session name is alluser, labelproperty = "name" is the stuff displayed in the drop-down list box, and property = "ID" is the stuff hidden from each piece of data in the drop-down list box. use property = "selecteduser" to obtain the selected data.

<HTML: Text name = "sampleform" property = "name"
Value = "<% = (fwthreadcontext. getattribute (" username "). tostring () %>"/>
It is easy to set the session name username to the text box. You can still get it through the name in form.

Tag book:
1, lable
<Bean: write name = "sampleform" property = "name"/>
2, text
<HTML: Text name = "sampleform" property = "name"/>
3, button
<HTML: Submit property = "button">
<Bean: Message key = "label. Button. Save"/>
</Html: Submit>
<HTML: button property = "button" onclick = "javascript: opencalendar (date);">
<Bean: Message key = "label. Button. Date"/>
</Html: button>
4, select
<HTML: Select Property = "selecteduser">
<HTML: Options name = "sampleform" collection = "userlist" property = "ID" labelproperty = "name"/>
</Html: Select>
5, checkbox, multibox
<HTML: checkbox name = "sampleform" property = "chechbox1" value = "true"/>

<Logic: iterate name = "sampleform" id = "user" property = "userlist">
<HTML: multibox property = "selectedusers">
<Bean: write name = "user" property = "ID"/>
</Html: multibox>
<Bean: write name = "user" property = "name"/>
</Logic: iterate>

6. Cyclic Logic
<Logic: present name = "sampleform" property = "userlist">
<Logic: iterate id = "user" name = "sampleform" property = "userlist">
<Tr>
<TD>
<HTML: Radio name = "sampleform" property = "selecteduserid" value = "<% = (JP. co. mhcb. obs. persis. entity. user) pagecontext. getattribute ("user ")). GETID (). tostring () %> "/>
</TD>
<TD> <Bean: write name = "user" property = "ID"/> </TD>
<TD> <Bean: write name = "user" property = "name"/> </TD>
</Tr>
</Logic: iterate>
</Logic: Present>

7. If Logic
<Logic: equal name = "sampleform" property = "showallflg" value = "true">
<HTML: Submit property = "button">
<Bean: Message key = "label. Button. All"/>
</Html: Submit>
</Logic: equal>
<Logic: equal name = "sampleform" property = "showallflg" value = "false">
<HTML: Submit property = "button">
<Bean: Message key = "label. Button. noall"/>
</Html: Submit>
</Logic: equal>
 

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.