) Struts-HTML: Use of link labels

Source: Internet
Author: User

<HTML: link> tags are used to generate HTML <A> elements. <HTML: link> A hyperlink has two advantages:
(1) request parameters can be included in URLs in multiple ways.
(2) When the user's browser closes the cookie, the URL will be automatically rewritten and sessionid will be included in the URL as the request parameter to track the user's session status.

<HTML: link> tags have the following important attributes:
(1) Forward: Specifies the Global Forwarding link.
(2) href: Specifies the complete URL key.
(3) page: Specify the URL relative to the current webpage.

<HTML: rewrite> used to output the URI part of the hyperlink, but it does not generate HTML <A>
Element. Uri refers to the protocol, host, and port in the URL. Uri
Used to specify specific request resources. For example, for URL: http: // localhost: 8080/htmlbasic. Do, its URI is
/Htmlbasic. Do

Example:
1. Create a Global Forwarding Link
First, define a <forward> element in the <global-forwards> element in the Struts-config.xml:
<Global-forwards>
<Forward name = "Index" Path = "/index. jsp"/>
</Global-forwards>
Then, create the <HTML: link> tag in the JSP file:
<HTML: link forward = "Index">
Link to global actionforward
</Html: link>
The forward attribute of the <HTML: link> tag matches the <forward> sub-element in the <global-forwards> element. The above code generates the following HTML content:
<A href = "/index. jsp"> link to global actionfoward </a>

It is worth noting that the forward attribute of <HTML: link> references only the Struts-config.xml
The <forward> sub-element in the configuration file <global-forwards>. If the <action>
The <forward> sub-element in will throw an exception during running:
Cannot create rewrite URL: Java. net. malfomedurlexception: cannot retrieve actionforward

2. Create a link with a complete URL
If the web application needs to be linked to another site, the complete URL of the other sites should be provided, for example:
<HTML: link href = "http://jakarta.apache.org/struts/index.html">
Generate an "href" directly
</Html: link>
The generated HTML code is as follows:
<A href = "http://jakarta.apache.org/struts/index.html"> Generate an "href" directly </a>
It is worth noting that if the href attribute of the <HTML: link> tag is specified, even if the cookie of the user's browser is disabled, <HTML: the link> tag does not add the user sessionid as the request parameter to the URL.

3. Create a relative URL from the current webpage
To link a webpage to another webpage in the same application, use the following method:
<HTML: link page = "/htmlbasic. Do">
A relative link from this page
</Html: link>
<HTML: link> the page attribute of a tag is used to specify the URI relative to the current application. The above code generates the following HTML content:
<A href = "/lib/htmlbasic. Do"> ...... </a>

4. Include Request Parameters in URLs or Uris
To include request parameters in a URL or Uri, you only need to add the request parameters to the end of the URL or Uri. For example:
<HTML: link page = "/htmlbasic. do? Prop1 = ABC & amp; prop2 = 123 ">
Hard-code the URL parameters
</Html: link>
<! -- Or -->
<HTML: rewrite page = "/htmlbasic. do? Prop1 = ABC & amp; prop2 = 123 "/>
The above code generates the following HTML content:
<A href =/lib/htmlbasic. do? Prop1 = ABC & amp; prop2 = 123 ">... </a>
Rewrite:/htmlbasic. do? Prop1 = ABC & amp; prop2 = 123

Tip: & amp represents the special character "&" in HTML "&"

5. contain a single request variable in a URL or Uri

If you want to include a request parameter in the URL, and the value of this number of people exists in a variable accessible to the current webpage, you can do so as follows.
To demonstrate this function, first create a variable that is accessible to the current webpage. For example, two variables are created in this example. One is a character type and the other is mermerbean, which are stored in a page range:
<%
/*
* Create A String object to store as a bean in
* The page content and embed in this link
*/
String stringbean = "value to pass ont URL ";
Pagecontext. setattribute ("stringbean", stringbean );
%>
<JSP: usebean id = "customerbean" Scope = "page" class = "htmltaglibs. Beans. curstomerbean"/>
<JSP: setproperty name = "customerbean" property = "name" value = "weiqin"/>
Then, add these two variables to the URL or Uri as request parameters:
<HTML: link page = "/htmlbasic. Do"
Paramid = "urlparamname"
Paramname = "stringbean">
URL encode a parameter based on a string bean Value
</Html: link>
<HTML: link page = "/htmlbasic. Do"
Paramid = "urlparamname"
Paramname = "customerbean"
Paramproperty = "name">
URL encode a parameter based on a customer bean Value
</Html: link>

Rewrite: <HTML: rewrite page = "/htmlbasic. Do"
Paramid = "urlparamname" paramname = "stringbean"/>
Rewrite: <HTML: rewrite page = "/htmlbasic. Do"
Paramid = "urlparamname" paramname = "customerbean"
Paramproperty = "name"/>

<HTML: link> the paramid attribute of the tag specifies the request parameter name, And the paramname attribute specifies the variable name. If the variable is JavaBean, use the paramproperty property to specify the JavaBean attribute.
For the stringbean in this example, the request parameter value is the string value of stringbean. For customerbean, The paramproperty attribute is specified, and the request parameter value is the name attribute value of customerbean.
The above code generates the following HTML content:
<A href = "/htmlbasic. do? Urlparamname = value to pass on URL ">
URL encode a paramter based on a string bean Value
</A>

<A href = "/htmlbasic. do? Urlparamname = weiqin ">
URL encode a parameter based on a customer bean Value
</A>

Rewrite:/htmlbasic. do? Urlparamname = value to pass on URL
Rewrite:/htmlbasic. do? Urlparamname = weiqin
6. Multiple request variables are included in the URL or Uri.
If a URL or Uri contains multiple Request Parameters whose values come from multiple variables, you must first define a map-type Java class, such as Java. util. hashmap, which is used to store request variables. For example:
<%
/*
* Strore values int A Map (hashmap in this case)
* And construct the URL Based on the map
*/
Java. util. hashmap mymap = new java. util. hashmap ();
Mymap. Put ("mystring", new string ("mystringvalue "));
Mymap. Put ("myarray", new string [] {"str1", "str2", "str3 "});
Pagecontext. setattribute ("map", mymap );
%>
In the haspmap of the above Code, two objects are stored, and the second object is a string array. Hashmap is stored in pagecontext. Next we can add this hashmap as the request parameter to the URL or Uri:
<% -- For this version of the <HTML: link> tag: -- %>
<% -- Map = a map with name/value pairs to pass on the URL -- %>
<HTML: link page = "/htmlbasic. Do" name = "map">
URL encode a parameter based on value in a map
</Html: link>
<% -- Create the same rewrite string for the above link. -- %>
Rewrite: <HTML: rewrite page = "/htmlbasic. Do" name = "map"/>

<HTML: link> the name attribute of the tag specifies the hashmap object containing the request variable. Each pair of "key/value" in the hashmap object represents one or more "request parameter name/request parameter value ". The above code generates the following HTML content:
<A href = "/htmlbasic. do? Mystring = mystringvalue & amp; myarray = str1 & amp; myarray = str2 & amp; myarray = str3 ">
URL encode a parameter based on value in a map
</A>

Rewrite:/htmlbasic. do? Mystring = mystringvalue & amp; myarray = str1 & amp; myarray = str2 & amp; myarray = str3

 

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.