[Struts] Hello World Demo

Source: Internet
Author: User

Struts is a framework based on the MVC pattern. Struts 2 is not the next version of struts and almost rewrites struts. Struts referred to in this article refer to Struts 2.

Model, which maintains data for the application, returns data according to the View's request, and updates the data according to the Controller's instructions

View, displaying the data in the specified format, controlled by the controller

Controller, receiving input from the user, validating the validity of the input data, then responding to the business logic processing and changing the Model data status, and finally displaying the data through the View

Is the Model,view,controller highly abstract diagram of struts 2. A user request will probably go through the next few processes

    • The user sends a page request to the server in the browser
    • Filterdispatcher response action based on the requested URL
    • Interceptors (interceptors) to verify input, upload files, etc.
    • Perform response actions
    • The interceptor works again, making the required post-calibration
    • View render results page and return the page to the user

Code directory structure

Web. XML is the entry point for the project.

Struts.xml configures the URL and Java logic code, as well as the mapping of the view JSP file.

Logging.properties Configuration logging Feature

INDEX.JSP is the default page file for the project

Helloworldaction.java, Business logic code

Helloworld.jsp, after the Helloworldaction.java has finished processing the business logic, the page is displayed

Code details

Web. XML, all requests sent to Struts 2 will be processed by Web. XML first. This is handled by Strutsprepareandexecutefilter. The Welcome page for this example is the index.jsp file

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:web= "Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"ID= "webapp_id"version= "3.0">      <Display-name>Struts 2</Display-name>   <welcome-file-list>      <Welcome-file>index.jsp</Welcome-file>   </welcome-file-list>     <Filter>      <Filter-name>Struts2</Filter-name>      <Filter-class>Org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</Filter-class>   </Filter>   <filter-mapping>      <Filter-name>Struts2</Filter-name>      <Url-pattern>/*</Url-pattern>   </filter-mapping></Web-app>

Struts.xml, this example

Associating the URL index with the view file index.jsp

Associate the URL hello with the model file Helloworldaction.java, as well as the view file helloworld.jsp. When the Helloworld.execute method executes the result of success Yes, the view file helloworld.jsp is called.

Opening Struts.devmode will display and develop the relevant log information.

The package element is used to categorize the request, where the index action and the Hello action are attributed to the HelloWorld package.

<?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>    <constantname= "Struts.devmode"value= "true" />    < Packagename= "HelloWorld"extends= "Struts-default">        <Actionname= "Index">            <result>/index.jsp</result>        </Action>        <Actionname= "Hello"class= "Dwptest.helloworldaction"Method= "Execute">            <resultname= "Success">/helloworld.jsp</result>        </Action>    </ Package></Struts>

Index.jsp This is a simple JSP file. The Hello action of the form is mapped to the Helloworldaction.execute method on Struts 2

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@ taglib Prefix="s"URI="/struts-tags"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Title here</title></Head><Body>    <H1>Hello World from Struts2</H1>    <formAction= "Hello">        <label for= "Name">Pls enter your name</label>         <inputtype= "text"name= "Name" />         <inputtype= "Submit"value= "Say hello" />    </form></Body></HTML>

Helloworldaction.java,action is a key part of Struts 2, and most of the business logic is implemented in the Action class. Here is a simple example where the Execute method returns directly to success. During the run, Struts 2 creates a Helloworldaction object and invokes its Execute method to respond to the user's request.

 Packagedwptest; Public classhelloworldaction {PrivateString name;  PublicString Execute ()throwsexception{return"Success"; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}

helloworld.jsp, the Taglib command tells the servlet container to use the tag of Struts 2 for processing. S:property Displays the action class property, where name is returned by Helloworldaction.getname

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@ taglib Prefix="s"URI="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Submit Result</title></Head><Body>Hello World<S:propertyvalue= "Name"/></Body></HTML>

The jar file used in this example is as follows

Resources

Struts 2-hello World Example, Tutorialspoint

[Struts] Hello World Demo

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.