Chapter 4 step into the Hall of struts2 framework development

Source: Internet
Author: User

Step 1: Download struts2



Struts download home page: http://struts.apache.org/download.cgi download the latest available, personal use is struts-2-0-11.2-all.zip

 

Step 2: directory structure

 

 

Folder name folder content

 

 

 

Apps 4 war package instance applications with source code for readers to read the source code

Backport is used for core class libraries and conversion tools of JDK 1.4.

Docs javadocand on-line document delivery, you can double-click index.html to start reading

All core class libraries and dependency packages of LIB struts 2

SRC Source Code

Build struts2 Development Environment

 

Step 1: Create a project



Create a dynamic web project named strutslogin

Src directory: stores all source code of Web applications.

Webcontent Directory: the directory of the current web application, which can store JSP and HTML pages.

Webcontent/WEB-INF Directory: Stores project configuration files, etc.

Webcontent/WEB-INF/lib directory: The jar package that stores the web application library.

 

Step 2: add the struts2 jar package

Add 5 important jar packages


 

Jar Package Description

 

Struts2-core-2.0.11.2.jar struts2 core package

Xwork-2.0.5.jar xwork2 library, struts2 core package it as the underlying inventory in

Ognl-2.6.11.jar ognl is a powerful expression language similar to El

Freemarker-2.3.8.jar struts all UI tag templates are written using freemarker

The commons logging package of commons-logging-1.0.4.jar apanche encapsulates common log Interfaces

Step 3: Configure web. xml

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app version = "2.4" <br/> xmlns = "http://java.sun.com/xml/ns/j2ee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee <br/> http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> </P> <p> <filter> <br/> <filter-Name> struts2 </filter-Name> <br/> <filter-class> <br/> Org. apache. struts2.dispatcher. filterdispatcher <br/> </filter-class> <br/> </filter> </P> <p> <filter-mapping> <br/> <filter-Name> struts2 </filter-Name> <br/> <URL-pattern>/* </url-pattern> <br/> </filter-mapping> </P> <p> <welcome-file-List> <br/> <welcome-File> login. JSP </welcome-File> <br/> </welcome-file-List> <br/> </Web-app>

 

Step 4: Create the struts configuration file: struts. xml

Right-click SCR and create 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"> <br/> <struts> <br/> <constant name = "struts. enable. dynamicmethodinvocation "<br/> value =" false "/> <br/> <constant name =" struts. devmode "value =" false "/> <br/> <! -- Add packages here --> <br/> <! -- <Br/> <include file = "tgis. XML "> </include> <br/> <include file =" Manager. XML "> </include> <br/> --> <br/> <package name =" amigo "extends =" struts-Default "> <br/> <action name = "login" class = "amigo. struts. login. action. loginaction "> <br/> <result>/success. JSP </result> <br/> <result name = "input">/login. JSP </result> <br/> </Action> <br/> </package> <br/> </struts> <br/>

 

Struts2 enables user logon

Step 1: Compile the login. jsp logon page

Create a JSP under webcontent

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "%> <br/> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <br/> <HTML> <br/> <pead> <br/> <title> logon page </title> <br/> </pead> <br/> <body> <br/> <Div align =" center "> <br/>$ {requestscope. message} <br/> <s: Form Action = "login" method = "Post"> <br/> <s: textfield name = "loginname" size = "20" label = "username"/> <br/> <s: password name = "password" size = "20" label = "password"/> <br/> <s: Submit value = "Submit"> </s: submit> <br/> </S: Form> <br/> </div> <br/> </body> <br/> </ptml>Step 2: Compile the login success page success. jsp

Create a JSP under webcontent

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "%> <br/> <HTML> <br/> <pead> <br/> <title> logon success page </title> <br/> </ head> <br/> <body> <br/> login successful !! <Br/> </body> <br/> </ptml>

Step 3: Write the login actionloginaction

1. Create the amigo. Struts. login. action package under SRC.

2. Create a class: loginaction. Java to inherit the actionsupport class

Package amigo. struts. login. action; <br/> Import COM. opensymphony. xwork2.actionsupport; <br/> public class loginaction extends actionsupport {<br/> Public String loginname; <br/> Public String password; <br/> Public String message; <br/> Public String getloginname () {<br/> return loginname; <br/>}< br/> Public void setloginname (string loginname) {<br/> This. loginname = loginname; <br/>}< br/> Public String GetPassword () {<br/> return password; <br/>}< br/> Public void setpassword (string password) {<br/> This. password = password; <br/>}< br/> Public String getmessage () {<br/> return message; <br/>}< br/> Public void setmessage (string message) {<br/> This. message = message; <br/>}< br/> @ override <br/> Public String execute () throws exception {<br/> // todo auto-generated method stub <br/> If ("amigo ". equals (loginname) & "amigo ". equals (password) {<br/> message = loginname + "Login successful"; <br/>}else {<br/> message = loginname + "Login Failed "; <br/> return input; <br/>}< br/> return success; <br/>}</P> <p >}< br/>

 

Step 2: Modify struts configuration file: 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"> <br/> <struts> <br/> <constant name = "struts. enable. dynamicmethodinvocation "<br/> value =" false "/> <br/> <constant name =" struts. devmode "value =" false "/> <br/> <! -- Add packages here --> <br/> <! -- <Br/> <include file = "tgis. XML "> </include> <br/> <include file =" Manager. XML "> </include> <br/> --> <br/> <package name =" amigo "extends =" struts-Default "> <br/> <action name = "login" class = "amigo. struts. login. action. loginaction "> <br/> <result>/success. JSP </result> <br/> <result name = "input">/login. JSP </result> <br/> </Action> <br/> </package> <br/> </strut

 

Step 5: Run login. jsp



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.