cool hit counter [SSH Fast Forward] - A simple example of struts2_Intefrankly

[SSH Fast Forward] - A simple example of struts2


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/47048123

Recently just started struts2, here to do a simple struts2 example to learn together with you.

This example implements the simplest login, containing only two pages: login.jsp for entering the user name and password; success.jsp for the login success page; error.jsp for the login failure page.


1、New web project "struts2"


2、Introduce jar package


Download the required jar packages for struts2 struts-2.3.24-all.zip

After unpacking, import the following minimal jar package into WebRoot/WEB-INF/lib

3. Create a page under WebRoot


login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>  
  <body>
  <form action="login">
    username:<input type="text" name="username" /><br>
    password:<input type="password" name="password" /><br>
    <input type="submit" value="submit" /><br>
  </form>   
  </body>
</html>

Here the value of the action attribute of the form tag points to the name attribute of the action tag in struts.xml below, indicating that the submitted form is handled by the LoginAction class pointed to by the class in the action tag.

success.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
   Congratulations!:${requestScope.username}  Landing successful<br/>
  </body>
</html>

error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head> 
  <body>
     landing failure
  </body>
</html>

3. Configure web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    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">
  <display-name></display-name> 
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>

  <!--  thisFilter The purpose is to set up all requests to be made byStruts2 come to terms with -->
  <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><!--  All requests are made byStruts2 come to terms with -->
  </filter-mapping>
</web-app>

thisin officefilter The filter sets up any client requests to be answered by thestruts2 come to terms with( set upstruts For the request dispenser)

4、Create action


package com.danny.user.action;

public class LoginAction {
    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;
    }

    public String execute() {
        if("admin".equals(username) && "admin".equals(password))
        {
            return "success";
        }else{
            return "error";
        }
    }
}

This LoginAction is used to process the data sent from the login page. Since it has to process data, it first has to receive data, so how does it receive the data from the foreground? After the page is submitted, go to struts.xml to find the corresponding action, and then find the LoginAction, LoginAction according to the name attribute of the input form to get the value, the rule is: the name attribute value of the input should be the same as the corresponding get and set methods in the LoginAction, and no relationship with the fields of the LoginAction: for example, the name attribute value of the input is "username", then the get method name of the corresponding field in the LoginAction should be "getUsername()". So this LoginAction could have been changed to look exactly like this.

package com.danny.user.action;

public class LoginAction {
    private String name;
    private String pwd;

    public String getUsername() {
        return name;
    }

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

    public String getPassword() {
        return pwd;
    }

    public void setPassword(String password) {
        this.pwd = password;
    }

    public String execute() {
        if("admin".equals(name) && "admin".equals(pwd))
        {
            return "success";
        }else{
            return "error";
        }   
    }
}

The execute method in LoginAction is a method that every action must implement to process data and return the interface to be displayed.

5, configure struts.xml


Create struts.xml in the src directory

<?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="struts2" extends="struts-default">
        <!-- extends It means inheritance.,struts-default situated instruts2-core-2.3.24.jar.struts-default.xml -->
            <action name="login" class="com.danny.user.action.LoginAction">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
            </action>
        </package>          
    </struts>

In struts.xml, each action corresponds to an action, and the action with the name "login" corresponds to the LoginAction under the com.danny.user.action package. The name of the result corresponds to the return value of the action's execute, if it returns "success" then it jumps to success.jsp; if it returns "error" then it jumps to error.jsp.


until this, Deployment projects, interviewslocalhost:8080/struts2, Go to the login page:

On successful login, jump to the success page at

When username and password are wrong, it jumps to the login failure page at.

The whole process is as follows


Recommended>>
1、A mustsee 2018 development direction for beauty industry people to stop being confused
2、Selected Blockchain Information for March 15 2018
3、Ningxia Zhongwei Cloud Computing Data Center Air Conditioning Design
4、Scientists around the world raise concerns about the birth of genetically programmed babies
5、Programmers have these expectations for Python in 2018

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号