Struts2 simple example, struts2 example

Source: Internet
Author: User

Struts2 simple example, struts2 example

Today, I wrote a struts2 example to help you understand the basic process of struts2. In fact, the framework is not as difficult as you think. To put it bluntly, the essence of struts2 is a large Servlet, that is, the part that originally needs to be submitted to the Servlet for processing is now handed over to the common Class for processing through the configuration file.

First, create a javaWeb project, import the packages that struts2 depends on to lib (you can download the packages from Baidu or the official website), and then go to the web. configure struts2 in xml. The content added is as follows:

1 <filter>2     <filter-name>struts2</filter-name>3     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>4 </filter>5 <filter-mapping>6     <filter-name>struts2</filter-name>7     <url-pattern>/*</url-pattern>8 </filter-mapping>

Write the following code in index. jsp:

1 <form method = "post" action = "testAction"> 2 Name: <input type = "text" name = "name"/> 3 <input type = "submit" value = "submit"/> 4 </form>

Next, write the struts configuration file struts. xml. The content is as follows:

 1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 3  "http://struts.apache.org/dtds/struts-2.1.dtd"> 4 <struts> 5     <package name="testaction" namespace="/" extends="struts-default"> 6         <action name="testAction" class="com.struts.action.TestAction"> 7             <result name="success">/success.jsp</result> 8             <result name="error">/index.jsp</result> 9         </action>10     </package>11 </struts>

One action corresponds to a response. In index. jsp, action = "testAction". Therefore, the name = "testAction" of the action is a one-to-one correspondence. Then, the class corresponds to the person who handles the request. Based on the configuration file, we create a new TestAction class in the com. struts. action package. The content of this class is as follows:

 1 package com.struts.action; 2  3 public class TestAction { 4     private String name; 5     public String getName() { 6         return name; 7     } 8     public void setName(String name) { 9         this.name = name;10     }11     public String execute(){12         if ("username".equals(name)) {13             return "success";14         } else {15             return "error";16         }17         18     }19 }

Here, we need to write an execute method whose return value is String type. Here, return corresponds to struts. the name attribute of the result in xml, and struts. the content of the result in xml corresponds to the corresponding page.

The field name corresponds to index. name in jsp. Generally, we use request in servlet. getParameter ("name") is obtained, but in struts2, we only need to encapsulate the field, and the rest will be handed over to struts2.

Of course, the other problem is that the execute method is the default method. If our method name is not execute, we need. add a property: method = "corresponding method name" to the action in xml, so that struts2 will call the corresponding method of the corresponding class.

In row 12th of TestAction, I just made a simple judgment. You can connect to the database as needed for a login 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.