JSP (7) -- configuration and use of struts 2.1.6

Source: Internet
Author: User

This article does not describe what struts is, nor do you want to introduce the magic of Struts. It only discusses how to configure struts and use struts.

Learning Materials: http://struts.apache.org/2.1.6/index.html

If you need to understand the background of struts, you can go to the official website to see the user manual. If you need to start using struts, you can download it from the official website;

I would like to remind you that the content on the official website is the most direct and I can also question what I wrote, this is just my understanding of struts.

The starting point of this article is that you have downloaded the complete struts version.

Address: http://apache.etoak.com/struts/binaries/struts-2.1.6-all.zip

> I started my struts journey.

1. My environment Introduction

The configuration of the Tomcat environment will not be repeated here. I used to write an article

[Jdk1.6 + tomcat6.0 configuration development JSP] http://blog.csdn.net/rocket5725/archive/2009/03/17/3999180.aspx

It is important to understand my environment, because only by understanding my environment can you see the information provided by the configuration file and the text entered in the address bar.

The browsing address: http: // localhost: 8080/APP ensures that the website is normally browsed. The project name is app, which is definitely different from yours. Several apps will appear in the subsequent configuration process. You can simply replace them with your project. Of course, understanding the principles is the most important thing. Imitation is only the first step.

Your first step is to enter http: // localhost: 8080/yourapp in the address bar to display a simple page.

2. Prepare the struts2 installation package and dependency package.

Decompress the downloaded Struts-2.1.6 file and find the following package file in Lib:Struts2-core-2.0.11.1.jar, xwork-2.0.4.jar, commons-logging-1.0.4.jar, freemarker-2.3.8.jar, ognl-2.6.11.jarAnd copy these packages to APP/web_inf/lib.

3. Install struts

This step is required for both struts1.x and struts2, but the installation method is different. The entry point of struts1 is a servlet, And the entry point of struts2 is a filter ). Therefore, struts2 needs to be configured as a filter. The following code configures struts2 under the <web-app> node in Web. xml:

<Filter> <br/> <filter-Name> struts2 </filter-Name> <br/> <filter-class> Org. apache. struts2.dispatcher. filterdispatcher </filter-class> <br/> </filter> </P> <p> <filter-mapping> <br/> <filter-Name> struts2 </Filter -Name> <br/> <URL-pattern>/* </url-pattern> <br/> </filter-mapping>

4. Compile the action class

This step and struts1.x must also be performed. The struct class in struts1.x must be inherited from the action class, while the struct class of struts2.x must be inherited from the com. opensymphony. xwork2.actionsupport class. The following is the code for calculating two integers and the action class. The Code is as follows:

Package COM. gslsoft. test; <br/> Import COM. opensymphony. xwork2.actionsupport; </P> <p> public class firstaction extends actionsupport <br/>{< br/> private int operand1; <br/> private int operand2; <br/> Public String execute () throws exception <br/>{< br/> If (getsum ()> = 0) // if the number of codes and non-negative integers, jump to positive. JSP page <br/>{< br/> return "positive"; <br/>}< br/> else // if the number of codes and values are negative integers, jump to negative. JSP page <br/>{< br/> return "negative"; <br/>}< br/> Public int getoperand1 () <br/>{< br/> return operand1; <br/>}< br/> Public void setoperand1 (INT operand1) <br/>{< br/> system. out. println (operand1); <br/> This. operand1 = operand1; <br/>}< br/> Public int getoperand2 () <br/>{< br/> return operand2; <br/>}< br/> Public void setoperand2 (INT operand2) <br/>{< br/> system. out. println (operand2); <br/> This. operand2 = operand2; <br/>}< br/> Public int getsum () <br/>{< br/> return operand1 + operand2; // calculate the number of codes and <br/>}</P> <p >}< br/>

From the code above, we can see that one feature of the explain class is to overwrite the execute method, but the execute method of struts2 has no parameters, and the execute method of struts1.x has four parameters. The return values of the execute method are also different. Struts2 returns only one string to express the execution result (a flag ). The rest of the code above will be explained below.

5. Compile the actionform class

In this example, you must use actionform. In struts1.x, you must create an actionform class (or define an action form) separately. In struts2, actionform and action are already integrated. From the code in step 2, we can see that the subsequent part should be written in the actionform class. Therefore, in step 2, The actionform class in this example has been compiled (that is, the second half of the action class ).

6. Configure the action class

This step struts1.x and struts2.x are required, but the configuration file in struts1.x is generally called a struts-config.xml (of course it can also be another file name), and generally put in the WEB-INF directory. The configuration file in struts2.x is generally struts. XML, and the Struts. xml file is created in the app/WEB-INF/classes directory. The following code configures the lifecycle class in struts. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype struts Public <br/> "-// Apache Software Foundation/DTD struts configuration 2.0/EN" <br/> "http://struts.apache.org/dtds/struts-2.0.dtd"> </P> <p> <struts> </P> <p> <constant name = "struts. enable. dynamicmethodinvocation "value =" false "/> <br/> <constant name =" struts. devmode "value =" false "/> </P> <p> <include file =" example. XML "/> </P> <p> <package name =" default "namespace ="/APP "extends =" struts-def Ault "> <br/> <default-action-ref name =" Index "/> <br/> <action name =" sum "class =" com. gslsoft. test. firstaction "> <br/> <result name =" positive ">/positive. JSP </result> <br/> <result name = "negative">/negative. JSP </result> <br/> </Action> </P> <p> </package> </P> <p> <! -- Add packages here --> </P> <p> </struts> <br/>

7. Compile the user input interface: Create a JSP page

(1) On the homepage (sum. jsp), create the sum. jsp page under the root directory. The Code is as follows:

<% @ Page Language = "Java" contenttype = "text/html; charset = GBK" pageencoding = "GBK" %> <br/> <% @ page import = "Java. util. * "%> <br/> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <br/> <HTML> <br/> <pead> <br/> <title> input Operations </title> <br/> </pead> <br/> <body> <br/> evaluate algebra and <br/> <br/> <s: form Action = "app/sum. action "> <br/> <s: textfield name =" operand1 "label =" operand 1 "> </S: textfield> <br/> <s: textfield name = "operand2" label = "operand 2"> </S: textfield> <br/> <s: Submit value = "Algebra and"> </s: submit> <br/> </S: Form> <br/> </body> <br/> </ptml>

(2) create a positive. jsp page

<% @ Page Language = "Java" contenttype = "text/html; charset = GBK" pageencoding = "GBK" %> <br/> <% @ page import = "Java. util. * "%> <br/> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <br/> <HTML> <br/> <pead> <br/> <title> show integers and </title> <br/> </pead> <br/> <body> <br/> algebra and non-negative integers <p> <s: property value = "sum"/> </p> <br/> </body> <br/> </ptml>

(3) create a new nagetive. jsp page

<% @ Page Language = "Java" contenttype = "text/html; charset = GBK" pageencoding = "GBK" %> <br/> <% @ page import = "Java. util. * "%> <br/> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <br/> <HTML> <br/> <pead> <br/> <title> show integers and </title> <br/> </pead> <br/> <body> <br/> algebra and non-negative integers <p> <s: property value = "sum"/> </p> <br/> </body> <br/> </ptml>

The implementation code of the two JSP pages is basically the same. Only one <s: Property> label is used to display the sum attribute value in the action class.

Restart Tomcat and type http: // localhost: 8080/APP/SUM. jsp in the address bar to see the struts implementation effect.

Figure 1: sum. jsp page

>

 

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.