My first struts Program (1)

Source: Internet
Author: User

I have just started learning struts recently. I plan to write down the learning process and some of my experiences. On the one hand, I want to encourage myself to gain a little bit every day. On the other hand, I would like to ask you to give me more advice, either technically or technically.

I also started from the simplest and most classic things. I personally think this example is the most representative.

1. Create two JSP files for page display

Login. jsp (login page)
---------------------

<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Taglib uri = "/tags/Struts-bean" prefix = "Bean" %>
<% @ Taglib uri = "/tags/Struts-logic" prefix = "logic" %>
<% @ Taglib uri = "/tags/Struts-html" prefix = "html" %>
<% @ Taglib uri = "/tags/Struts-nested" prefix = "nested" %>
<HTML: HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK"/>
<Title> </title>
</Head>
<Body>
<HTML: Form method = "Post" Action = "loginaction. Do">
<HTML: Text property = "name"> <HTML: Password property = "password"> <HTML: Submit> </Html: Form>
</Body>
</Html: HTML>

On this page, enter the user form and submit the user information.

Note that the property attributes of the form element text and password must be the same as those in the Form class, because after the form is submitted, Struts follows the content of the form element, automatically assign values to the corresponding fields in the Form class.

Index. jsp (the page is displayed after successful login)
------------------------------
<% @ Page pageencoding = "GBK" contenttype = "text/html; charset = GBK" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK"/>
<Title> </title>
</Head>
<Body>
Succ!
</Body>
</Html>

On this page, only information about successful login is displayed.

2. Compile the loginactionform class

Package test;

Import org. Apache. Struts. Action. actionform;

Public class loginactionform extends actionform {
String name;
String password;
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public String GetPassword (){
Return password;
}
Public void setpassword (string password ){
This. Password = password;
}

}

Note that the name and password attributes must be the same as those in the login. jsp file.

3. Compile the loginaction class

Package test;

Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Import org. Apache. Struts. action. Action;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionforward;
Import org. Apache. Struts. Action. actionmapping;

Public class loginaction extends action {

Public actionforward execute (actionmapping arg0, actionform arg1, httpservletrequest arg2, httpservletresponse arg3) throws exception {
Loginactionform = (loginactionform) arg1;
String name = loginactionform. getname ();
String Password = loginactionform. GetPassword ();
If (name. Equals (password ))
Return (arg0.findforward ("succ "));
Else
Return (arg0.findforward ("fail "));
}
}

This class is the most important class in struts and mainly completes the processing of business logic. A simple judgment is made here. If the user name and password are equal, the logon success page is switched; otherwise, the logon Failure page is redirected.

 

4. Configure the struts-config.xml File

<? XML version = "1.0"?>
<! Doctype Struts-config public "-// Apache Software Foundation // DTD struts configuration 1.2 //" http://struts.apache.org/dtds/struts-config_1_2.dtd ">
<Struts-config>
<Data-sources>
</Data-sources>
<Form-beans>
<Form-bean name = "loginactionform" type = "test. loginactionform"> </form-bean>
</Form-beans>
<Global-exceptions>
</Global-exceptions>
<Global-forwards>
</Global-forwards>
<Action-mappings>
<Action Path = "/loginaction" name = "loginactionform" type = "test. loginaction">
<Forward name = "succ" Path = "/index. jsp"> </forward>
<Forward name = "fail" Path = "/login. jsp"> </forward>
</Action>
</Action-mappings>
<Controller processorclass = "org. Apache. Struts. Tiles. tilesrequestprocessor"/>
<Message-resources parameter = "messageresources"/>
<Plug-in classname = "org. Apache. Struts. Tiles. tilesplugin">
<Set-Property = "definitions-config" value = "/WEB-INF/tiles-defs.xml"/>
<Set-Property = "moduleaware" value = "true"/>
</Plug-in>
<Plug-in classname = "org. Apache. Struts. validator. validatorplugin">
<Set-Property = "pathnames" value = "/WEB-INF/validator-rules.xml,/WEB-INF/validation. xml"/>
</Plug-in>
</Struts-config>

In this file, the configuration to be filled in is

1:

<Form-beans>
<Form-bean name = "loginactionform" type = "test. loginactionform"> </form-bean>
</Form-beans>
This form bean assigns values to bean attributes based on each element of the Form Content submitted by the user.

2:

<Action-mappings>
<Action Path = "/loginaction" name = "loginactionform" type = "test. loginaction">
<Forward name = "succ" Path = "/index. jsp"> </forward>
<Forward name = "fail" Path = "/login. jsp"> </forward>
</Action>
</Action-mappings>

Here, the path attribute in the Action tag is used to match the request of the action.

In login. jsp, Action = "loginaction. Do" matches loginaction

The name attribute indicates the called actionform class, which is of course the form bean.

The forward label specifies the matching of the redirection. In loginaction

If (name. Equals (password ))
Return (arg0.findforward ("succ "));
Else
Return (arg0.findforward ("fail "));

"Succ" and "fail" correspond to the name attribute of forword, while the path attribute specifies the path to the page.

In this way, the first struts program is complete. When you log on, enter the same user name and password, and succ is displayed! Prompt information. Otherwise, the original logon page is returned.

The above mainly describes some of the problems I noticed while doing this, and may also be some of the most common mistakes for beginners.

End!

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.