struts2 framework entry-level Demo (simple user login)

struts2 framework entry-level Demo


I have just finished the integrated development of jsp + servlet. I just started struts2 today and wrote an entry-level demo. Novices can take a look. I think it is very helpful for further study of the struts2 framework in the future.

amount! The struts2 framework, which belongs to the c (controller controller) control layer in the mvc framework, replaces a lot of the original working methods that use jsp. For a detailed description of struts2, readers can Baidu by themselves.

Ok, then start to explain the demo.


First of all, if you need to use the struts2 framework in the web project, you must configure web.xml. (Only you can use it if you configure it in web.xml!) The configuration code is given below

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Struts2Demo</display-name>
  <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>

Define the processor tag filter, filtername specifies the name of the tag struts2, filterclass specifies the processing class of struts2. The filter-map specifies that the effective range of struts2 is effective for all items in the web application.


The next step is to configure struts.xml and attach the code:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

          <package name="gang" namespace="/" extends="struts-default">
        <action name="login" class="gang.LoginAction">
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result name="success">/WEB-INF/content/welcome.jsp</result>
        </action>
     </package>

    <!-- 第二个程序 跳转到登录页面 -->
    <package name="chen" namespace="/" extends="struts-default">
        <action name="*">
            <result>WEB-INF/content/{1}.jsp</result>
        </action>

    </package>
</struts>

Configure struts.xml. The package tag is used to define a processing block of struts, where the name defines the name of the block, the namespace defines the namespace, and extends defines the processing block it inherits.

The action tag defines the behavior, name is the name of the behavior, and class is the class that executes the behavior.

The result tag is used to jump to the specified view page for the result returned by the class specified by class.


After configuring two xml configuration files. First, write a simple jsp login page.

<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login">
    <s:textfield name="username" key="用户名"/>
    <s:textfield name="password"  key="密码"/>
    <s:submit key="登录"/>
</s:form>
</body>
</html>

The jsp page is very simple. However, pay attention to the use of struts2 tags, so first declare
<% @ taglib prefix = ”s” uri = ”/ struts-tags”%> to import tag libraries.

Action = login in the definition form. Well, JSP is so much.


The next step is to create the action class, the most critical step.
Paste the code first.

package gang;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

    //定义请求参数的username和password
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    //定义用户请求的executor方法
    @Override
    public String execute() throws Exception {
        if(getUsername().equals("chengang")
                &&getPassword().equals("123456"))
        {
            ActionContext.getContext().getSession()
            .put("user", getUsername());
            return SUCCESS;
        }
        return ERROR;

    }

}

Define a class named loginaction. He inherits the actionsupport class. The
first step is to define the username and password of the request parameters.

// Step executor method requested by the user defined
class code is very simple. Determine whether the user name and password entered by the user are correct and return success, and error returns error. Then pass it to struts.xml to jump to the corresponding result according to the returned parameters.


Ok, basically get it. The page about a successful jump and a page that fails to jump are handled by the reader's own needs. It is very simple.

The novice demo ends.

Note: struts.xml should be placed in the src directory, which is the same directory as the java package. The java package in this article is named chen, and all the jsp in this article are in the WEB-INF / content directory.

—-jeker-chen
2016/5/20

Published 26 original articles · praised 1 · visits 9796

Guess you like

Origin blog.csdn.net/qq_31884013/article/details/51458652