Struts2的缺省Action处理

本文介绍如何在Struts2框架中配置一个通用Action来动态处理视图展示,通过将请求URL转换为实际的JSP路径,简化页面访问流程。

在Struts2中,若你的所有页面都放在pages这样的目录下,并且没有进行特别的配置,而你在对外访问的时候,不想通过jsp,而是使用Struts的action来进行处理,这样,即所有的jsp都需要配置一个对应的Action.

假设我配置了Struts2的扩展名为.do.

 

http://localhost:8080/myapp/admin/main.do希望是访问我的web/pages/admin/main.jsp

 

这样一来,所有的页面的显示,均需要配置一个action.有时,我们仅是需要显示一个信息提示的页面,并不需要作页面的数据处理,这时,我们可以配置一个公共的Action作为这些访问的缺省处理。该处理则仅显示对应的jsp即可。

 

 

1.在Struts.xml中配置一个能动态处理的Action.该Action包括一个动态的视图属性,如

<?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>
 <constant name="struts.action.extension" value="do" />
 <constant name="struts.devMode" value="true" />
 <constant name="struts.ui.theme" value="xhtml"/>
 <constant name="struts.custom.i18n.resources" value="conf/message" />

 <package name="default" extends="struts-default">
   <default-action-ref name="defaultAction"/>
   <action name="defaultAction" class="com.ht.web.action.BaseAction">
    <result>${successResultValue}</result>
   </action>
 </package>

</struts>

 

其中successResultValue为BaseAction中的属性。

 

如下为BaseAction的实现:

import org.apache.struts2.dispatcher.DefaultActionSupport;
public class BaseAction extends DefaultActionSupport{
	@Override
	public String execute() throws Exception {
		HttpServletRequest request=getRequest();
		String uri=request.getRequestURI();
		String url=uri.substring(request.getContextPath().length());
		url=url.replace(".do", ".jsp");
		url="/pages"+url;
		setSuccessResultValue(url);
		return SUCCESS;
	}
}

 其中DefaultActionSupport的代码如下:

/*
 * $Id: DefaultActionSupport.java 471756 2006-11-06 15:01:43Z husted $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.struts2.dispatcher;


import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * A simple action support class that sets properties to be able to serve
 */
public class DefaultActionSupport extends ActionSupport {

    private static final long serialVersionUID = -2426166391283746095L;

    private String successResultValue;


    /**
     * Constructor
     */
    public DefaultActionSupport() {
        super();
    }

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.ActionSupport#execute()
     */
    public String execute() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        String requestedUrl = request.getPathInfo();
        if (successResultValue == null) successResultValue = requestedUrl;
        return SUCCESS;
    }

    /**
     * @return Returns the successResultValue.
     */
    public String getSuccessResultValue() {
        return successResultValue;
    }

    /**
     * @param successResultValue The successResultValue to set.
     */
    public void setSuccessResultValue(String successResultValue) {
        this.successResultValue = successResultValue;
    }


}

 

 

因此大家可以看出,successResultValue可以为动态的result结果页。

所以在BaseAction中的execute,则把逻辑视图的url,转成真正的物理视图jsp,设置至successResultValue变量中去,然后再由struts的ActionMapping去处理。

提供了基于BP(Back Propagation)神经网络结合PID(比例-积分-微分)控制策略的Simulink仿真模型。该模型旨在实现对杨艺所著论文《基于S函数的BP神经网络PID控制器及Simulink仿真》中的理论进行实践验证。在Matlab 2016b环境下开发,经过测试,确保能够正常运行,适合学习研究神经网络在控制系统中的应用。 特点 集成BP神经网络:模型中集成了BP神经网络用于提升PID控制器的性能,使之能更好地适应复杂控制环境。 PID控制优化:利用神经网络的自学习能力,对传统的PID控制算法进行了智能调整,提高控制精度稳定性。 S函数应用:展示了如何在Simulink中通过S函数嵌入MATLAB代码,实现BP神经网络的定制化逻辑。 兼容性说明:虽然开发于Matlab 2016b,但理论上兼容后续版本,可能会需要调整少量配置以适配不同版本的Matlab。 使用指南 环境要求:确保你的电脑上安装有Matlab 2016b或更高版本。 模型加载: 下载本仓库到本地。 在Matlab中打开.slx文件。 运行仿真: 调整模型参数前,请先熟悉各模块功能输入输出设置。 运行整个模型,观察控制效果。 参数调整: 用户可以自由调节神经网络的层数、节点数以及PID控制器的参数,探索不同的控制性能。 学习修改: 通过阅读模型中的注释查阅相关文献,加深对BP神经网络与PID控制结合的理解。 如需修改S函数内的MATLAB代码,建议有一定的MATLAB编程基础。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值