SSH Fast Advanced--struts2 Simple Example

Source: Internet
Author: User

Recently just getting Started struts2, here is a simple struts2 example to learn with you.

This example realizes the simplest landing, contains only two pages: Login.jsp used to enter the user name and password; success.jsp is the landing Success page; error.jsp is the landing failure page.

1. New Web project "Struts2"

2. Introduction of JAR Package

Download the struts2 required Jar pack Struts-2.3.24-all.zip

After decompression, the following basic jar package is imported into Webroot/web-inf/lib

3, set up the page under Webroot

LOGIN.JSP:

<%@ page language="java" import="java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" ><html>  <head>  </head>    <body>  <form Action="Login">Username<input type="text" name="username" /><br>Password<input type="password" name="password" /><br>    <input type="Submit" value="Submit" /><br>  </form>     </body></html>

SUCCESS.JSP:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>  </head>  <body>  恭喜您:${requestScope.username} 登陆成功<br/>  </body></html>

error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>  </head>   <body>    登陆失败  </body></html>

3. Configure Web. xml

<?xml version= "1.0" encoding= "UTF-8"?><web-app version="2.5"xmlns="Http://java.sun.com/xml/ns/javaee"  xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">                 <display-name></display-name>   <welcome-file-list>    <welcome-file>login.jsp</welcome-file>  </welcome-file-list>  <!--This filter is to set all requests handled by STRUTS2-  <filter>    <filter-name>Struts2</filter-name>    <filter-class>Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>Struts2</filter-name>    <url-pattern>/*</url-pattern><!--All requests are handled by STRUTS2.  </filter-mapping></Web-app>

4. Create action

Package com.danny.user.action; Public classloginaction {PrivateString username;PrivateString password; PublicStringGetUserName() {returnUsername } Public void Setusername(String username) { This. Username = Username; } PublicStringGetPassword() {returnPassword } Public void SetPassword(String password) { This. Password = password; } PublicStringExecute() {if("Admin". Equals (username) &&"Admin". Equals (password)) {return "Success"; }Else{return "Error"; }    }}

5, Configuration Struts.xml

Build struts.xml in src directory

<?xml version= "1.0" encoding= "UTF-8"?><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.org/dt Ds/struts-2.3.dtd ">    <struts>        < package name="struts2" extends= "struts-default">        <!--extends is inherited meaning, Struts-default is located struts2-core-2.3.24.jar.struts-default.xml --and            <action name="Login" class="Com.danny.user.action.LoginAction" >            <result name="Success">/success.jsp</result>            <result name="error">/error.jsp</result>            </Action>        </Package >              </struts>

Now, deploy the project, access the LOCALHOST:8080/STRUTS2, and go to the landing page:

When the login succeeds, jump to the Success page:

User name, password error, jump to the landing failure page:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

SSH Fast Advanced--struts2 Simple Example

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.