3、struct2的常见配置

1、在eclipse中如何复制一个工程作为一个新的工程

在struct.xml中:

<result name="success">/login_sucess.jsp</result>

默认result的那么缺省值就是success,上面的语句等价于

<result >/login_sucess.jsp</result>

 2、在上面一节中我们编写的action

package com.weiyuan.test;

/**
 * struct2的action可以不继承任何框架的接口
 * 1、默认调用的是execute()方法
 * */
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() throws Exception{
        System.out.println("execute is called");
        System.out.println("execute is called" +username);
        System.out.println("execute is called"+password);
        if("admin".equals(username)&&"admin".equals(password)){
            return "success";
        }else{
            return "error";
        }
    }

}

没有实现任意的接口,需要自己手动书 public String execute() throws Exception方法,如果写错了就会出现问题,为了解决我们可以让自己编写的ation实现Action这个接口,这个接口默认继承类需要实现 public String execute() throws Exception这个方法,还提供了一些常量

/*
 * Copyright (c) 2002-2007 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.xwork2;

/**
 * All actions <b>may</b> implement this interface, which exposes the <code>execute()</code> method.
 * <p/>
 * However, as of XWork 1.1, this is <b>not</b> required and is only here to assist users. You are free to create POJOs
 * that honor the same contract defined by this interface without actually implementing the interface.
 */
public interface Action {

    /**
     * The action execution was successful. Show result
     * view to the end user.
     */
    public static final String SUCCESS = "success";

    /**
     * The action execution was successful but do not
     * show a view. This is useful for actions that are
     * handling the view in another fashion like redirect.
     */
    public static final String NONE = "none";

    /**
     * The action execution was a failure.
     * Show an error view, possibly asking the
     * user to retry entering data.
     */
    public static final String ERROR = "error";

    /**
     * The action execution require more input
     * in order to succeed.
     * This result is typically used if a form
     * handling action has been executed so as
     * to provide defaults for a form. The
     * form associated with the handler should be
     * shown to the end user.
     * <p/>
     * This result is also used if the given input
     * params are invalid, meaning the user
     * should try providing input again.
     */
    public static final String INPUT = "input";

    /**


使用接口可以便于代码编程的规范,定义一些列常量和对应的方法,比如mqtt模块就可以将host admin,等常见定义到接口中,实现和业务的具体分开
package com.weiyuan.test;

import com.opensymphony.xwork2.Action;

/**
 * struct2的action可以不继承任何框架的接口
 * 1、默认调用的是execute()方法
 * */
public class LoginAction implements Action {
    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() throws Exception{
        System.out.println("execute is called");
        System.out.println("execute is called" +username);
        System.out.println("execute is called"+password);
        if("admin".equals(username)&&"admin".equals(password)){
            return "success";
        }else{
            return "error";
        }
    }

}



     * The action could not execute, since the
     * user most was not logged in. The login view
     * should be shown.
     */
    public static final String LOGIN = "login";


    /**
     * Where the logic of the action is executed.
     *
     * @return a string representing the logical result of the execution.
     *         See constants in this interface for a list of standard result values.
     * @throws Exception thrown if a system level exception occurs.
     *                   <b>Note:</b> Application level exceptions should be handled by returning
     *                   an error value, such as <code>Action.ERROR</code>.
     */
    public String execute() throws Exception;

}

我们的action如下

package com.weiyuan.test;

import com.opensymphony.xwork2.Action;

/**
 * struct2的action可以不继承任何框架的接口
 * 1、默认调用的是execute()方法
 * */
public class LoginAction implements Action {
    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() throws Exception{
        System.out.println("execute is called");
        System.out.println("execute is called" +username);
        System.out.println("execute is called"+password);
        if("admin".equals(username)&&"admin".equals(password)){
            return "success";
        }else{
            return "error";
        }
    }

}





2、常用配置2:如何在开发阶段,我们修改了struct.xml文件,我们不重启就可以让配置文件的修改有效了有下面的两种方式,注意在正式的运营环境最好不要配置该参数
第一:在工程的src目录下新建文件
struts.properties
配置文件的内容为
struts.configuration.xml.reload=true

第二种方式:在struct.xml的<structs>标签下配置该属性
<?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>
<!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 需要继承extends="struts-default",否则struct2的拦截器就无法使用 -->
<package name="struct2"  extends="struts-default">
   <action name="login" class="com.weiyuan.test.LoginAction">
      <result name="success">/login_sucess.jsp</result>
      <result name="error">/login_error.jsp</result>
   </action>
</package>

</struts>

3、struct2如何给页面更加有效的错误的提示在struct.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>
<!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 会提供更加友好的提示信息 -->
<constant name="struts.devMode" value="true"/>
<!-- 需要继承extends="struts-default",否则struct2的拦截器就无法使用 -->
<package name="struct2"  extends="struts-default">
   <action name="login" class="com.weiyuan.test.LoginAction">
      <result name="success">/login_sucess.jsp</result>
      <result name="error">/login_error.jsp</result>
   </action>
</package>

</struts>


总结:

1、<result>标签的name属性,如果不配置,那么缺省值为success


2、Struts2提供了一个Action接口,在Action接口中定义了一些常量和execute方法
我们可以使用该接口,这样开发更规范

3、struts2的常用配置参数
* struts.configuration.xml.reload
--当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置
* struts.devMode
--会提供更加友好的提示信息

以上参数配置方式有两种:
* 在struts.properties文件中配置
* 在struts.xml配置文件中




 
 
原文地址:https://www.cnblogs.com/kebibuluan/p/8026185.html