STRUTS2 data Label (i)

Source: Internet
Author: User

I. STRUTS2 data labels are primarily used to provide a variety of data access related functions, including displaying properties in an action, and generating internationalized output. Data labels include: property, set, Push, Param, bean, action, include, URL, i18n and text, date and debug tags, and more.



1.property tag: Used to output the value specified by the Value property. Can be used in conjunction with <s:bean> tags, one is to assign a value to the bean, and the other is to read the value from the bean.

(1). If you do not specify the Value property, the default output valuestack (value stack) stack at the top, it has the following properties:

-default: Optional attribute that outputs the value specified by the default property if the value you want to output is null.

-escape: Optional property that specifies whether to escapehtml code.

-value: Optional attribute that specifies the value of the property to output, the default output valuestack the top of the stack.

-id: Optional attribute that specifies the ID that references the element.

(2). Next attach an example, create a new STRUTS2 project, the project name is Datalabeltest, when using the Struts2 tag, be sure to add a tag library, that is, add this line of code:

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

-Create a new login page under the Webroot directory login.jsp, click the login button, jump to a login action, which also uses the property tag display prompt information, the page 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" >

-Create a new action class, Loginaction class, Put under the Com.action package, where if we enter the user name XG, the password is 123, return the string inherited from the Actionsupport class success, if it is not return error, and prompt login failure information, the Loginaction class code is as follows:

Package Com.action;import Com.opensymphony.xwork2.actionsupport;public Class Loginaction extends Actionsupport { private string Username;private string Password;private string Tip;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 Gettip () {return tip;} public void Settip (String tip) {this.tip = tip;} Public String Execute () {if (Username.equals ("xg") && password.equals ("123")) {return SUCCESS;} else {Settip (" Login Failed "); return ERROR;}}}

-Next, configure the Struts.xml configuration file, if returned from the Loginaction class is success, that is, jump to the login_success.jsp page, return error, jump to the login.jsp page, the code is as follows:

<?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><package name= "Default" namespace= "/" extends= "Struts-default" ><action name= " Login "class=" com.action.LoginAction "><result name=" Success ">/login_success.jsp</result>< Result name= "Error" >/login.jsp</result></action></package></struts>

-Finally, attached to the login_success.jsp page after successful login, using the property tag to display the user name, the 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+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


-Deploy this project to the Tomcat server, open the Tomcat server, and run the following effect:

Username input xg, password input 123, press the login button, jump to login This action, display the user name, namely:

If the user name and password are entered incorrectly, they will jump to login.jsp and prompt for login failure:



2.set tag: Used to put a value in a specified range, such as application, session range, etc.

(1). The set label is useful in some cases when a complex expression is referenced more than once in a page or when the object graph depth of a value is very deep. For example: Person.worker.wife.parent.age, each access to this value is not only low performance, but also poor readability. To solve this problem, you can set the value to a new value and put it in a specific range.

(2). Set Label properties:

-name: Mandatory, regenerate the name of the new variable.

-scope: Optional, specifies the range in which the new variable is placed, which can accept a application, session, request, page, or action five value. If not specified, it is placed in the context of the stack, which is the default.

-value: Optional, specifies the value that will be assigned to the variable, or, if not specified, assigns the value of the top of the Valuestack stack to the new variable.

-id: Optional, specifies the reference ID of the element.

(3). Next attach an example, and also write the test code under the Datalabeltest project.

-First, create a new bean class, the person class, with two properties name and age, set the set and get methods for each of these two properties, and the code is as follows:

Package Com.bean;public class Person {private string name;private int age;public string getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}


-Next, create a new person_message.jsp tag, use the Bean data label to make a JavaBean instance, and place it in the stack context, which uses the Param data label to assign a value to the name and age property, Then use the set tag to put the Bean object into the Ognlcontext, and then use the property tag to display the code 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+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


-Finally, the results are as follows:


-Here, the default is to put the bean object in the context of the stack, if you want to save these values in the application or session range, and then display, you can change the main code on the above person_message.jsp page to:

Place the P-value of the stack context into the application range:

    <s:bean name= "Com.bean.Person" id= "person" >    <s:param name= "name" value= "' John '"/>    <s:param Name= "Age" value= "/>"    </s:bean>    <s:set name= "P" value= "#person" scope= "Application"/>    <s:property value= "#attr. P.name"/><br/>    <s:property value= "#attr. P.age"/><br/>    <s:debug></s:debug>  </body>

The scope property is specified here, changed to application, and obtained with #attr.p.name to get the name value.

The P-value of the stack context is placed in the session range and the OGNL expression is used to obtain the corresponding value:

    <s:bean name= "Com.bean.Person" id= "person" >    <s:param name= "name" value= "' John '"/>    <s:param Name= "Age" value= "/>"    </s:bean>    <s:set name= "P" value= "#person" scope= "session"/> ${ Sessionscope.p.name}<br/> ${sessionscope.p.age}<br/>

The effect is the same.



3.push Tags: You can place a value on the top of the stack of Valuestack (value stack), which makes it easier to access the value.

(1). It has two properties:

-value: Required property that specifies the element to be placed at the top of the Valuestack (value stack) stack.

-id: Optional attribute that specifies the ID used to reference this label.

If you are working on an object more frequently, you can use the push tag to press the object to the top of the value stack, and then the action on that object can be simplified.

(2). Change the main code in the Person_message.jsp page above to read as follows:

    <s:bean name= "Com.bean.Person" id= "person" >    <s:param name= "name" value= "' Ann '"/>    <s:param Name= "Age" value= "/>    </s:bean><s:push value=" #person "><s:property value=" name "/>< Br/><s:property value= "Age"/></s:push>

Place the value of the person object at the top of the stack, as shown in the following:



4.param Tags: primarily used to provide parameters for other labels, such as append, merge, Bean, and include tags.

(1). The label has the following properties:

-name: Optional, specify parameter names that need to be set.

-value: Optional, specify parameter values that you want to set parameters for.

-id: Optional, specifies the ID that references the element.

(2). Usage:

-<s:param name= "name" value= "' Ann '"/>

-<s:param name= "Name" >Ann</s:param>



5.bean Tags: used to create an instance of a JavaBean. When you create an JavaBean instance, you can use the Param tag to pass in a property for the JavaBean instance within the tag, and you need to provide the corresponding setter method for the JavaBean class to use this tag. If the JavaBean class provides a corresponding getter method, the corresponding property can be accessed.

(1). Properties of the bean tag:

-name: Required, specifies the implementation class of the JavaBean to instantiate.

-id: Optional, if this attribute is specified, the JavaBean instance is placed in the stack context (in the case of the stacks), not the value stack (value stacks), allowing access to the JavaBean instance directly through the ID property.

(2). Using the bean tag Note: When inside the bean tag, the JavaBean instance created by the Bena tag is placed at the top of the Valuestack stack, and the resulting subset is moved out of the Valuestack stack unless the id attribute is specified.

(3). Next, attach an example using bean tags and param tags:

-We can create a new JavaBean class, the student class, which is placed under the Com.bean package, where the test code is also placed under the Datalabeltest project with the following code:

Package Com.bean;public class Student {private int id;//number private String name;//student name private int age;//student age Private Strin G sex;//Student Sex private String major;//student Professional public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} Public String Getmajor () {return major;} public void Setmajor (String major) {this.major = major;}}

-Then create a new page student_message.jsp with the following code:

<%@ 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+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


-the operating effect is as follows:


6. The above is the STRUTS2 in the data label some of the commonly used tags, there are some data labels, wait until the next time to write, so as not too long, please forgive me.



7. The above content is only for your study reference, write not good, please forgive me, if there are errors, please point out, thank you!



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

STRUTS2 data Label (i)

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.