Struts2的缺省Action处理

在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去处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值