JSP script, JSP declaration, JSP output expression, JavaScript, JSP tag, struts2 tag, struts2 action value passing Problem

Source: Internet
Author: User

During the development process, we often encounter a value transfer problem, which cannot be disturbed. Today we will write some understandings, and some may be wrong. Please also see the heroes to point out. I have used many books and online examples in this article. I would like to thank you and I will not mention them one by one.

1. define the concept in the question first.

(1) JSP script: in some cases, it is also called a Java Script, which is a Java Script on the JSP page that is enclosed by <%>. AllProgramExecuted inCodeAnd can all be executed through JSP scripts.

(2) JSP Declaration: Use <%! Declaration Part %> variables and Methods declared in the form. It can be directly used in JSP scripts.

(3) JSP output expression: Use <% = expression %> to output the expression value. The effect is the same as out. println In the JSP script. The expression cannot be followed by a semicolon.

(4) javascript: code between <SCRIPT> </SCRIPT> on the page, Js for short. JS inserts have different execution effects depending on different locations. It is executed only when it is put in the JS call in JavascriptThe suffix of a script file is. js. For example:

<Head>

<SCRIPT src = "example. js"> </SCRIPT>

</Head>

(5) it seems that there is no need to explain the remaining few. I skipped it here.

2. jsp scripts, JSP declarations, and JSP output expressions

The variables and Methods declared in the JSP declaration correspond to the member variables and methods in the servlet (the converted classes can be found in the Tomcat work \ Catalina \ localhost directory ). It can be directly used in JSP scripts and JSP output expressions.

Variables defined in JSP scripts can also be used in JSP output expressions. For example:

<! -- The following is the JSP declaration part -->
<%!
Public int count;
Public String Info ()
{Return "hello ";}
%>

<%
Integer I = 10;

Out. println (count ++ );
%>
<%
Out. println (Info ());
%>

<% = Count ++ %>

<% = Info () %>

<% = I %>

3. jsp tag, struts2 tag, and JavaScript

Use VaR orgip = Document. getelementbyid ("Address"). Value to obtain the tag value in JavaScript. Different tag methods may have different read values. For example, the autocompleter of struts2 needs to be obtained in the following way.

VaR completer = dojo. widget. byid ("name ");
VaR organizationname = completer. comboboxselectionvalue. value;

The getelementbyname user obtains a group of tags with the same name.

You can also set the tag value in JavaScript. For example:

Document. getelementbyid ("Code"). value = "test ";

4. Between JSP scripts and struts2 actions

Valuestack is used for JSP scripts to access the variables passed by struts2 action. For example:

<%
Valuestack Vs = (valuestack) request. getattribute ("struts. valuestack ");
User user = (User) vs. findvalue ("user ");

String name = vs. findvalue ("name ");

%>

Here, user and name are member variables defined in action, and corresponding getter and setter methods are available.

5. jsp tag, struts2 tag, and struts2 action

To access the values of JSP tags and struts2 tags, struts2 action requires a variable with the same name as the tag and corresponding getter and setter methods. This is the value transfer mechanism of struts2. Of course, the same name mechanism is also used when struts2 action is passed to the JSP tag and struts2 tag on the JSP page to which the result is redirected. Note that the value is transferred once. That is to say, if jsp1-> Action1-> jsp2, jsp1 can only pass the value to action1.

JSP tag and struts2 tag pass the value to action through form or S: Form on the page, the action passes the value to the JSP page by using the setter method or directly assigning values to the action member variable with the same name as the tag.

1) for JSP tags, you must use <s: Property> to access the value from the action. For example:

<Input name = "name" type = "text" class = "form_field_default" size = "50" id = "name" value = "<s: property value =" user. name "/>">

<Input name = "name" type = "text" class = "form_field_default" size = "50" id = "name" value = "<s: property value = "name"/> ">

The user passed by the former is an object and has the name attribute. The name passed by the latter is simple type data, such as string.

2) For the struts2 tag, you need to access the passed value through the % {Variable} method. For example:

<S: textfield name = "name" id = "name" size = "40" value = "% {user. name}"> </S: textfield>

<S: textfield name = "name" id = "name" size = "40" value = "% {name}"> </S: textfield>

6. Between JSP output expressions and struts2 actions

JSP output expressions can also directly output the values transmitted from struts2 action. For example:

<% = Pagenum * pagesize %>

Here, pagenum and pagesize are values transmitted from struts2 action.

7. access data from struts2 action through JavaScript

Use S: property to read data. In the following example, smsunit is a variable passed from action.

Document. getelementbyid ("smsunit"). value = "<s: property value =" smsunit "/>"

 

Think of this for the time being. If you miss it, add it later.

Related Article

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.