Strust basic configuration

Source: Internet
Author: User
Tags tld

This article from: http://blog.csdn.net/flying_huang/article/details/1504710

1. Download struts2.0.1

Bytes.

Ii. Try struts2.0.1

1. Create a new web project, copy all jar files under the lib directory of the struts-2.0.1-all.zip package to the/WEB-INF/lib directory of the web project.

Modify the Web. xml file under the WEB-INF to add the following:

<Filter> <filter-Name> struts2 </filter-Name> <filter-class> Org. apache. struts2.dispatcher. filterdispatcher </filter-class> </filter> <filter-mapping> <filter-Name> struts2 </filter-Name> <URL-pattern>/* </url-Pattern> </filter-mapping> <! -- Here is to set the struts2 label, you can also do not need to set, because the struts-core.jar directory in the META-INF already contains this TLD file, the J2EE container automatically loads it --> <JSP-config> <taglib-Uri>/S </taglib-Uri> <taglib-location>/WEB-INF/TLDs/ struts-tags.tld </taglib-location> </taglib> </JSP-config>

 

A filter of filterdispathcer of struts2 is defined in Web. xml. This filterdispatcher is used to initialize struts2 and process all Web requests.

2. Create a New login page login. jsp:

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <HTML> 

 

3. compile action login:

package org.rainlife.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {    private String username;    private String password;    @Override    public String execute() throws Exception {        if (!(getUsername().equals("rainlife"))                && !(getPassword().equals("rainlife"))) {            return ERROR;        } else {            return SUCCESS;        }    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }}

 

In this loginaction class, actionsupport is inherited. Actionsupport is a base class in the open-source framework xwork2 that allows action to work more quickly. It contains the default implementation of many optional services in action, it makes it easier for us to customize an action.

Here we define the username and password attributes and provide the corresponding get/set method. A execute () method is defined. This method overwrites the execute () method in the actionsupport class. As you can see, it simply returns a string ("success" or "input ", instead of returning an actionforward in struts1, the two strings are also defined in actionsupport. Four string attributes are defined in actionsupport, namely success, input, error, and login.

In this way, our action has been completed, but there is still a problem. How can we let struts2 know our custom action, and can I submit the action to the action on the HTML/JSP page? The answer is to configure the Struts. xml file.

4. Configure struts. xml

<?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>    <package name="struts2" extends="struts-default">        <action name="login" class="org.rainlife.struts2.action.LoginAction">            <result name="error">/error.jsp</result>            <result name="success">/success.jsp</result>                </action>    </package></struts>

 

In this struts. xml configuration file, it can be found that and the previous struts-config.xml has been completely different, and the configuration file in webwork is very similar. Here, we define an action named name = "login" and use the class attribute to point to the loginaction class we just created. In this way, we will tell struts2 the defined action. On the HTML/JSP page, you can use the "login" name to submit the action to the corresponding action.

If the namespace attribute is set in the package, such as namespace = "/struts2", the form action should be set to "/struts2/login. Action" on the JSP page ".

5. Create error. jsp and success. jsp:

Error. JSP <% @ page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <HTML> 

 

 

Struts2 matches the string defined by the name attribute in <result> In struts. XML based on the string (error or success) returned in loginaction, and jumps to the corresponding page.

Strust basic configuration

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.