The first struts2 instance:

Source: Internet
Author: User
The first struts2 instance:
Development tools and languages for this example: Eclipse 3.2 + struts2 + Tomcat 5.5
The user login function is implemented. Because the database is not connected, you can log in with a fixed user name and password.

Username: luanmad
Password: Admin

Files used in this instance and their structures: (see Figure 1)
//************************************** *********************************
K:/eclipseworks/myfirststruts2
│. Classpath (automatically generated by ECLIPSE)
│. Mymetadata (automatically generated by ECLIPSE)
│. Project (automatically generated by ECLIPSE)

─ ──. Myeclipse (automatically generated by ECLIPSE)
├ ── SRC
│ Struts. xml

│ ─ ── CN
│ ─ ── Struts2
│ Loginaction. Java

└ ── Webroot
│ Login. jsp
│ Loginfailure. jsp
│ Loginsuccess. jsp

├-META-INF
│ Manifest. MF

└-WEB-INF
│ Web. xml

Category-classes
│ Struts. xml

│ ─ ── CN
│ ─ ── Struts2
│ Loginaction. Class

└ ── Lib
Commons-logging-1.0.4.jar
Freemarker-2.3.8.jar
Ognl-2.6.11.jar
Struts2-core-2.0.11.2.jar
Xwork-2.0.5.jar

//************************************** *********************************

1. The specific configuration of Tomcat 5.5 is described here. You can search for it online)
The Tomcat 5.5 file used in this instance configuration is server. xml.

Find the conf folder in the Tomcat 5.5 directory that you have installed. There is a folder named server. XML file (for example, C:/tomcat 5.5/CONF/server. XML), open the file for editing, and add a sentence between <Context Path = "/myfirststruts2" docbase = "K:/eclipsworks/myfirststruts2/webroot" DEBUG = "0" reloadable = "true" crosscontext = "true">
</Context>

Let Tomcat 5.5 find your startup program;

2. Write the loginaction. Java file to process the logon routine. In (myfirststruts2/src/CN/struts2/loginaction. Java)
Loginaction. Java
//************************************** *********************************
Package CN. struts2;

Import com. opensymphony. xwork2.actionsupport;

Public class loginaction extends actionsupport
{
Private string name;
Private 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;
}

Public String execute () throws exception
{
String Path = NULL;
If (name. Equals ("luanmad") & password. Equals ("admin "))
{
Path = success;
}
Else
{
Path = error;
}
Return path;
}

// Rewrite a validate () function, which is used to verify user input information. It is executed before the execute () function.
@ Override
Public void validate ()
{
// Verify that the user name and password are empty
If (null = Name | "". Equals (name ))
{
This. addfielderror ("name", "Name is required! ");
}

If (null = PASSWORD | "". Equals (password ))
{
This. addfielderror ("password", "password is required! ");
}

}

}

//************************************** *********************************

3. Configure the Struts. xml file (src/struts. XML)
Struts. xml
//************************************** *********************************

<? 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>

<Package name = "struts2" extends = "struts-Default">
<Action name = "login" class = "cn. struts2.loginaction">
<Result name = "success">/loginsuccess. jsp </result>
<Result name = "error">/loginfailure. jsp </result>
<Result name = "input">/login. jsp </result>
</Action>
</Package>

</Struts>
//************************************** *********************************

4. Configure the Web. xml file (webroot/WEB-INF/Web. XML)
Web. xml
//************************************** *********************************

<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>

<Filter>
<Filter-Name> struts2 </filter-Name>
<Filter-class> org. Apache. struts2.dispatcher. filterdispatcher </filter-class>
</Filter>

<Filter-mapping>
<Filter-Name> struts2 </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
</Web-app>

//************************************** *********************************

5. Three. jsp files (in the webroot folder)

Login. jsp
//************************************** *********************************

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "GBK" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + "://"
+ Request. getservername () + ":" + request. getserverport ()
+ Path + "/";
%>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">

<Title> my JSP 'login2. jsp 'starting page </title>

<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->

</Head>

<Body>
<S: Form Action = "login" method = "Post">
<S: textfield name = "name" label = "User name:"> </S: textfield>
<S: password name = "password" label = "password"> </S: Password>
<S: Submit label = "Submit"> </S: Submit>
<S: reset label = "reset"> </S: reset>
</S: Form>
</Body>
</Html>

//************************************** *********************************

Loginsuccess. jsp
//************************************** *********************************

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "GBK" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + "://"
+ Request. getservername () + ":" + request. getserverport ()
+ Path + "/";
%>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">

<Title> my JSP 'login2. jsp 'starting page </title>

<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->

</Head>

<Body>
<H1> login successful! </H1>
<H2>
User name: <s: property value = "name"/> <br>
Password: <s: property value = "password"/>
</H2>
</Body>
</Html>

//************************************** *********************************

Loginfailure. jsp
//************************************** *********************************

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "GBK" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + "://"
+ Request. getservername () + ":" + request. getserverport ()
+ Path + "/";
%>

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">

<Title> my JSP 'login2. jsp 'starting page </title>

<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->

</Head>

<Body>
<H1> login failed! </H1>
<H2> the user name or password is incorrect! </H2>
</Body>
</Html>

//************************************** *********************************

6. Do not forget to import the struts2 jar package (use the five) (put in webroot/WEB-INF/lib/), this is required.
Commons-logging-1.0.4.jar
Freemarker-2.3.8.jar
Ognl-2.6.11.jar
Struts2-core-2.0.11.2.jar
Xwork-2.0.5.jar

7.1
At the same time, you need to import the above five jar packages into referced libraries, which is used for editing in eclipse.

8. This program runs normally. You can write it on your browser when the configuration is good.
HTTP: /localhost: 8080/myfirststruts2/login. jsp
(Port 8080 by default)

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.