Drafting Struts2

Studying Struts2

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>
  <!--Action package definition-->
  <package name="default" extends="struts-default">
    <!--Hello world sample action-->
    <action name="hello" class="sample.HelloWorldAction" method="execute">
      <result name="success">/view/sample/HelloWorld.jsp</result>
    </action>
</struts>

index.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!--Enable Struts2 tag library-->
<%@ taglib prefix="s" uri="/struts-tags"%>
<!--Use HTML5 type declaration-->
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
  </head>
<body>
  <!--Input form uses Struts2 tags-->
  <s:form action="hello">
    <s:textfield name="name" />
    <s:submit value="Go to Hello World page" />
  </s:form>
</body>
</html>

HelloWorldAction.java


package sample;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * [Hello World action class]<br>
 * <br>
 * @author tarosa0001
 */
public class HelloWorldAction {
    /**Logger*/
    private Logger logger = LogManager.getLogger(HelloWorldAction.class);

    /**name*/
    private String name;

    /**
     * [Action execution process]<br>
     *Execute the process to output Hello World.<br>
     *The method to execute is struts.Implement using the method name specified in xml.<br>
     *In Struts2, the page transition destination is determined by the processing result of the action,
     *This time"success"Returns the fixed character string of.<br>
     * <br>
     * @return
     */
    public String execute() {
        logger.info("The entered parameter is "{}"is.", name);
        return "success";
    }

    /**
     * [Name acquisition process]<br>
     *Get the name.<br>
     *When Struts2 uses the value entered in the form as a parameter
     *Since getter is used, the information you want to use as a parameter is
     *Declare it as a property of Action class and prepare getter.
     * <br>
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * [Name setting process]<br>
     *Set the name.<br>
     *Like getters, prepare setters for the values you want to pass between pages.<br>
     * <br>
     * @param name name
     */
    public void setName(String name) {
        this.name = name;
    }
}

HelloWorld.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Hello Struts2 world!</title>
  </head>
  <body>
    <!--Shows the data passed between messages.-->
    Hello Struts2 world, <s:property value="name" />Mr.
  </body>
</html>

web.xml


<?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>Struts2-01</display-name>
  <welcome-file-list>
    <!--Display file settings when habitually in the application root-->
    <!--It is not necessary, but this time, to prevent typos in the URL, index.Only jsp.-->
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!--Struts2 Servlet Filter-->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>

  <!--Filter settings to use for URLs-->
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Other

web.xml


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
  </head>
<body>
  <s:form action="sample_tag">
page<br/>
    s:textfield<s:textfield name="user_id" value="USER_ID" size="24" label="User ID"/><br/><br/>
    s:password<s:password name="password" value="PASSWD" size="24" label="password"/><br/><br/>
    s:hidden<s:hidden name="item_id" value="HIDDEN"/><br/><br/>
・ Select test(single)
    <select name="select1">
      <option value="1">To page of result 1</option>
      <option value="2">To page of result 2</option>
    </select><br/><br/>
・ Select test(Multi)
    <select name="select2" multiple="multiple">
      <option value="A">Type A</option>
      <option value="B">B type</option>
      <option value="O">O type</option>
      <option value="AB">AB type</option>
    </select><br/><br/>
・ Checkbox test
    <input type="checkbox" name="chbox1" value="1" checked="checked" />interesting
    <input type="checkbox" name="chbox1" value="2" />Useful
    <input type="checkbox" name="chbox1" value="3" checked="checked" />Not good enough
    <br/><br/>
・ Radio test
    <input type="radio" name="radio1" value="1" checked="checked" />good
    <input type="radio" name="radio1" value="2" />bad
    <br/><br/>
    <s:submit value="SUBMIT!!" />
  </s:form>
</body>
</html>

Sample01Action.java


package sam.tag;

/**
 * @author
 */
public class Sample01Action extends AbstractAction{

    private String user_id;
    private String password;
    private String item_id;

    private String select1;
    private String select2;

    private String chbox1;
    private String radio1;

    /**
     * [Action execution process]<br>
     * @return
     */
    public String execute() {
//        final String className = new Object(){}.getClass().getEnclosingClass().getName();
        final String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
//        System.out.println("className::" + className);
//        System.out.println("methodName :" + methodName);

        logger.info("▼ Start ▼:{}",methodName);
        logger.info("The entered parameter is "{}"is.", toString());

        short nextPage = Short.parseShort(select1);

        try {
            if (nextPage == 1) {
                return "success1";
            } else {
                return "success2";
            }
        } catch (Exception e) {
            return "error";
        } finally {
            logger.info("▲ End ▲: execute()");
        }
    }

    @Override
    public String toString() {
        return "Sample01Action [user_id=" + user_id + ", password=" + password + ", item_id=" + item_id + ", select1="
                + select1 + ", select2=" + select2 + "]";
    }

    //The following automatically generated getters and setters
    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getPassword() {
        return password;
    }

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

    public String getItem_id() {
        return item_id;
    }

    public void setItem_id(String item_id) {
        this.item_id = item_id;
    }

    public String getSelect1() {
        return select1;
    }

    public void setSelect1(String select1) {
        this.select1 = select1;
    }

    public String getSelect2() {
        return select2;
    }

    public void setSelect2(String select2) {
        this.select2 = select2;
    }

    public String getChbox1() {
        return chbox1;
    }

    public void setChbox1(String chbox1) {
        this.chbox1 = chbox1;
    }

    public String getRadio1() {
        return radio1;
    }

    public void setRadio1(String radio1) {
        this.radio1 = radio1;
    }

}

result1.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Result page</title>
  </head>
  <body>
Result 1 page<br/>
    s:textfield:<s:property value="user_id" /><br/>
    s:password:<s:property value="password" /><br/>
    s:hidden:<s:property value="item_id" /><br/>
    select1:<s:property value="select1" /><br/>
    select2:<s:property value="select2" /><br/>
    checkbox:<s:property value="chbox1" /><br/>
    radio:<s:property value="radio1" /><br/>
  </body>
</html>

Recommended Posts

Drafting Struts2
Drafting log4j2