Ssh-struts2 Getting Started instance

Source: Internet
Author: User

STRUTS2 is an implementation of MVC thought, which is similar to the MVC in. NET when learning struts2. STRUTS2 also has its own controller that understands that the controller in the Struts2 is the method with the action.
An instance is a log-in that logs input to an action and jumps to another form via action.

LOGIN.JSP, used to provide an input box and specify the action name (login).

<%@ page language="java" contenttype="text/html; Charset=iso-8859-1 "pageencoding="iso-8859-1"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" ><html><head><meta http-equiv="Content-type" Content="text/html; Charset=iso-8859-1 "><title>Insert Title here</title></head><body><form Action="Login">Username<input tyep="text" name="username"><br>Password<input type="password" name="password"><br>    <input type="Submit" value="Submit"></form></body></html>

Web. XML sets an interceptor. Incorporate the request into the filter jurisdiction.

<?xml version= "1.0" encoding= "UTF-8"?><web-app version="2.5"xmlns="http://java.sun.com/xml/ns/ Java ee "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" >                   <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>    </filter-mapping></Web-app>

Struts.xml, feel it more like a place to provide a map, find the name of the action request (login), and then jump to the specified background of the class, but also in the configuration file is configured in the return to success when the page needs to jump.

<?xml version= "1.0" encoding= "UTF-8"?><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "Http://struts.apache.org/dt Ds/struts-2.0.dtd "><struts>    < package name="struts2" extends= "struts-default">        <action name="Login" class="Com.syq.struts2.LoginAction" Method="Myexecute">            <result name="Success">/result.jsp</result>        </Action></struts>

LOGINACTION.JSP provides the Execute method, which will look for this method when the page jumps to the background, for further processing, which can be compared with the controller in. NET, particularly like.

 PackageCom.syq.struts2;ImportCom.opensymphony.xwork2.ActionSupport; Public  class loginaction extends actionsupport{    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(){returnSUCCESS; }}

result.jsp, specifies the page to which sucess jumps after executing the Execute method if it is returned.

<%@ page language="java" contenttype="text/html; Charset=iso-8859-1 "pageencoding="iso-8859-1"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" ><html><head><meta http-equiv="Content-type" Content="text/html; Charset=iso-8859-1 "><title>Insert Title here</title></head><body>Username:${requestscope.username}<br>Password:${requestscope.password}<br></body></html>

The process of the instance:
1. User clicks Form login.jsp request Resource "login"
2. The request path matches the Interceptor/* in Web. XML,
3. Incorporated into the jurisdiction of the filter org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.
4. Run to Struts.xml and look for the name "login" in the action. The action may have more than one.
5. Instantiate the class corresponding to the action, call the various set methods, set the user-submitted form to the member variable, execute the Execute method, and return the string success
6. The returned string is compared to the string in result in success and struts.xml under action, to find the same string, go to the specified page
7. Use Requestscope to remove the corresponding value

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

Ssh-struts2 Getting Started instance

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.