An example of a form label for a UI tag in Struts2

Source: Internet
Author: User

1. Recently wrote an article about the Struts2 UI tab in the form label, the article address is: http://blog.csdn.net/u012561176/article/details/44986183

Because of the lack of an example, we see the text and the table can not see what effect, so today to introduce an example, using the UI tag in the Struts2 form tag to implement a form of personal information, but some of the form label is not demonstrated, you can according to my UI tag of the form tag to learn, Here is just an example.


2. First create a new STRUTS2 project, the project name is Personmessage, the project structure is as follows:


(1). First create a new JSP page, the page name is form1.jsp, change the encoding method is utf-8, because we use the STRUTS2 UI tag, not the HTML tag, so you must use the Taglib compiler directive import Struts2 Tag Library definition, This line of code must be added at the top of the JSP page:

<%@ taglib uri= "/struts-tags" prefix= "s"%>


The complete code of the Form1.jsp page is as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><%@ taglib uri= "/struts-tags" prefix= "s" %><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/";%&GT;&L t;! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


Use the <s:form> tab in the Form1.jsp page above, and create some form elements using the form element tag for the user to enter or select the relevant content, use the <s:textfield> tag to complete the user name and the real name input, with < S:password> tags to complete the input of the password, with <s:radio> tags to complete the choice of gender, with <s:select> tags to complete the highest degree of choice, with <s:doubleselect> Label complete the province of the city selection, with <checkboxlist> tags to complete the choice of personal hobbies, with <s:combobox> tags to complete the choice of fruit like to eat, with <s:updownselect> The tab completes the preferred programming language selection, submits it with the <s:submit> tag, and submits it to the personaction action.


(2). Then create a new Personformaction class, declare some properties, and, for each of these properties, make a Get and set method, overriding the Execute () method of the Actionsupport class, with the complete code as follows:

Package Com.gk.action;import Com.opensymphony.xwork2.actionsupport;public Class Personformaction extends Actionsupport {private string username;//declares user Name property private string password;//declares password property private string truename;// Declare real name property private string sex;//declaration gender attribute private string education;//declaration education attribute private string province;//declaration province attribute private string CIT y;//declaration City attribute private string favorite;//declaration Hobby Property private string fruit;//declaration like to eat fruit attribute private string language;//declare your preferred programming language */* for On Polygon These properties create set and Get methods */public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public String Gettruename () {return truename;} public void Settruename (String truename) {this.truename = Truename;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} Public String geteducation () {return education;} public void Seteducation (String education) {this.Education = education;} Public String getprovince () {return province;} public void Setprovince (String province) {this.province = province;} Public String getcity () {return city;} public void Setcity (String city) {this.city = city;} Public String Getfavorite () {return favorite;} public void Setfavorite (String favorite) {this.favorite = favorite;} Public String Getfruit () {return fruit;} public void Setfruit (String fruit) {this.fruit = fruit;} Public String GetLanguage () {return language;} public void SetLanguage (String language) {this.language = language;} /** * Override the Execute () method of the Actionsupport class */public String execute () {return SUCCESS;}}



(3). Open the Struts.xml configuration file for the project directory and add the Personformaction configuration to the file, complete with the following code:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts public    "-//apache software foundation//dtd struts Configuration 2.0//en"    "/http Struts.apache.org/dtds/struts-2.0.dtd "><struts><constant name=" Struts.enable.DynamicMethodInvocation "value=" true "></constant><constant name=" Struts.devmode "value= "True" ></constant><constant name= "struts.custom.i18n.resources" value= "Globalmessages" ></ Constant><package name= "Person" namespace= "/" extends= "Struts-default" ><action name= "PersonAction" class= "Com.gk.action.PersonFormAction" ><result>/personinfo.jsp</result></action></ Package></struts>



(4). Then in the Webroot directory, create a new personinfo.jsp page, modify the encoding method to Utf-8, to submit the form to the action after the form input content, that is, get the Action property value, the complete code is as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><%@ taglib uri= "/struts-tags" prefix= "s" %><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/";%&GT;&L t;! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

If you assign a value using the form label and #{} This way, you can use the #parameters property name when you get the property.


(5). By writing the above 4-step code, the creation of the personal information form is completed, and the corresponding input content of the form can be obtained, then the project is deployed to the Tomcat server, the Tomcat server is opened, and the address bar is entered as shown in the following:

It appears that the interface can be entered and selected by ourselves, and I casually enter the content and select it, as shown in:

Then click the Submit button to get the input and select the content:



3. The above content is only for everybody to love the study reference, writes is not good, please forgive me, if has the mistake, please point out, thanks!

An example of a form label for a UI tag in Struts2

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.