Struts configuration in Java

  • 2020-04-01 03:57:25
  • OfStack

1. Understand the struts

The Struts2 framework USES packages to manage actions, interceptors, and so on. Each package is a collection of multiple actions, multiple interceptors, and multiple interceptor references.
The package element in the struts.xml file is used to define the package configuration, and each package element defines a package configuration. Its common attributes are:
L name: required property to specify the package name.
L extends: optional property that specifies that this package inherits other packages. Inheriting from other packages, you can inherit Action definitions, interceptor definitions, and so on from other packages.
L namespace: optional attribute that specifies the namespace of the package.

2. Configure struts

  Start by creating a new web project, right-click on a project, and select add struts under myeclipse
In select struts2.1 click next in select your desired package in save

3. Modify the user login verification example to add one more registered user function.

1.             Modify the Action class:


package org.qiujy.web.struts2.action;
 
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 

publicclass LoginAction extends ActionSupport{
  private String userName;
  private String password;
  
  private String msg; //Result information attribute
  
  
  public String getMsg() {
    returnmsg;
  }
  
  publicvoid setMsg(String msg) {
    this.msg = msg;
  }
  
  public String getUserName() {
    returnuserName;
  }
  
  publicvoid setUserName(String userName) {
    this.userName = userName;
  }
  
  public String getPassword() {
    returnpassword;
  }
  
  publicvoid setPassword(String password) {
    this.password = password;
  }
  
  
  public String login() throws Exception{
    if("test".equals(123) && "test".equals(123)){
      msg = " Login successful, welcome " + 123;
      //Get an ActionContext instance to access the Servlet API
      ActionContext context = ActionContext.getContext();
      //Check whether the user name has been stored in the session. If so, it means that the user has logged in.
//Otherwise, the login is successful for the first time
      if(null != context.getSession().get("uName")){
        msg = this.userName + " You've already logged in !!!";
      }else{
        context.getSession().put("uName", this.userName);
      }
      
      returnthis.SUCCESS;
    }else{
      msg = " Login failed, user name or password is wrong ";
      returnthis.ERROR;
    }
  }
  
  public String regist() throws Exception{
    //Add the username and password to the database
    //...
    msg = " Registration successful. ";
    returnthis.SUCCESS;
  }
}

2.             Struts.xml file: nothing changed, same configuration as before


<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <package name="my" extends="struts-default" namespace="/manage">
  <!--  Define the processing request URL for login.action the Action -->
    <action name="userOpt" class="org.qiujy.web.struts2.action.LoginAction">
    <!--  Defines the mapping between the processing result string and the resource  -->
      <result name="success">/success.jsp</result>
      <result name="error">/error.jsp</result>
    </action>
  </package>
</struts>

3.             Page:
The index. The JSP


<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
  <title> User login page </title>
</head>
 
<body>
 <h2> The user entry </h2>
 <hr>
  <form action="manage/userOpt!login.action" method="post">
  <table border="1">
     <tr>
       <td> User name: </td>
       <td><input type="text" name="userName"/></td>
     </tr>
     <tr>
       <td> Password: </td>
       <td><input type="password" name="password"/></td>
     </tr>
     <tr>
       <td colspan="2">
         <input type="submit" value="  determine  "/>
       </td>
     </tr>
  </table>
  </form>
</body>
</html>

Regist. JSP


<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
  <title> User registration page </title>
</head>
 
<body>
 <h2> User registration </h2>
 <hr>
  <form action="manage/userOpt!regist.action" method="post">
  <table border="1">
     <tr>
       <td> User name: </td>
       <td><input type="text" name="userName"/></td>
     </tr>
     <tr>
       <td> Password: </td>
       <td><input type="password" name="password"/></td>
     </tr>
     <tr>
       <td colspan="2">
         <input type="submit" value="  registered  "/>
       </td>
     </tr>
  </table>
  </form>
</body>
</html>

You can now use sturts.

The above is all the content of this article, I hope you can enjoy it.


Related articles: