struts2.3+spring3.2+urlrewrite零配置

首先给出struts.xml;其中有些配置不是必须,但是零配置是通过通配符来实现;所以这里以记录为主;

<?xml version="1.0" encoding="UTF-8" ?>
<!-- /* * $Id: struts-plugin.xml 722219 2008-12-01 20:41:26Z musachy $ * 
	* 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. */ -->

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<!-- 集成spring -->
	<constant name="struts.objectFactory" value="spring"></constant>
	<!-- 指定编码集 -->
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
	<constant name="struts.serve.static.browserCache" value="false" />
	<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
	<constant name="struts.configuration.xml.reload" value="true" />
	<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
	<constant name="struts.devMode" value="false" />
	<!-- 默认的视图主题 -->
	<constant name="struts.ui.theme" value="simple" />
	<!-- struts的action配置文件 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<!-- struts的请求未指明method时默认访问Controller/index -->
	<package name="default" namespace="/" extends="struts-default">

		<global-results>
			<result name="error">/error.jsp</result>
		</global-results>

		<global-exception-mappings>
			<exception-mapping exception="java.lang.Exception"
				result="error" />
		</global-exception-mappings>
<!-- struts2-spring集成零配置,结合url_rewrite -->
		<action name="*_*" class="{1}" method="{2}">
			<interceptor-ref name="defaultStack">
				<param name="modelDriven.refreshModelBeforeResult">true</param>
			</interceptor-ref>
			<result name="{2}">/pages/{1}/{1}-{2}.jsp</result>
		</action>
	</package>
	<!-- <include file="example.xml"/> -->


</struts>
controller代码
package com.cqta.dev.controller.demo;

import java.io.IOException;
import java.util.Collection;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.cqta.core.utils.ParamUtil;
import com.cqta.core.web.support.BaseController;
import com.cqta.dev.model.demo.Order;
import com.cqta.dev.service.demo.OrdersService;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.ValidationAwareSupport;

@Component("orders")
@Scope("prototype")
public class OrdersController extends BaseController implements ModelDriven<Object>, Validateable {
	private static final long serialVersionUID = 1L;
	private Order model = new Order();
	private Integer id;
	private Collection<Order> list;
	@Autowired
	public OrdersService ordersService;

	public String show() {
		return "show";
	}

	public String index() {
		list = ordersService.getAll();
		return "index";
	}

	public String edit() {
		return "edit";
	}

	public String editNew() {
		model = new Order();
		return "editNew";
	}

	public String deleteConfirm() {
		return "deleteConfirm";
	}

	public void delete() throws IOException {
		id = ParamUtil.getIntParam(request, "id", 0);
		if (id != null) {
			ordersService.delete(id);
		}
		redirect("index");
	}

	public void save() throws IOException {
		ordersService.save(model);
		redirect("index");
	}

	public void validate() {

		if (model.getClientName() == null || model.getClientName().length() == 0) {
		}
	}

	public void setId(Integer id) {
		if (id != null) {
			this.model = ordersService.get(id);
		}
		this.id = id;
	}

	/**
	 * 在执行到method之前,model对象(在getMode()方法中创建的对象)被压到ValueStack中,这时候,
	 * UserAction和ValueStack都指向同一个model对象;但紧接着,method中的model被一个新的user对象覆盖,这时候,
	 * UserAction和ValueStack不再指向同一个model对象
	 * !ValueStack中是旧的model对象,而method中是新的model对象
	 * !我们在JSP中,直接通过username/address等直接访问
	 * ,当然是要访问ValueStack中的旧model对象,所以它们的属性都是空的(id属性除外)!
	 */
	public Object getModel() {
		if (model != null && model.getId() != null) {
			model = ordersService.get(model.getId());
		}
		return (list != null ? list : model);
	}

}
urlrewrite.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
        "http://tuckey.org/res/dtds/urlrewrite3.2.dtd">

<!--

    Configuration file for UrlRewriteFilter
    http://tuckey.org/urlrewrite/
    Struts2 demo  urlRewrite,from springside3.1
<urlrewrite>
	<rule>
		<from>^/common/users.htm$</from>
		<to type="forward">/common/user.action</to>
	</rule>
	<rule>
		<from>^/common/user/([0-9]+)\.htm$</from>
		<to type="forward">/common/user!input.action?id=$1</to>
	</rule>
</urlrewrite>
-->
<urlrewrite>
	
	
    <rule>
		<note>
			如果url中包含".",则不对其进行重写,即对包含.*后缀的url不进行重写,以此来避免css、img等文件的正常加载
		</note>
            <from>/([.a-z_A-Z0-9/\-]+)[.]([a-z_A-Z0-9]+)</from>
            <to last="true" type="forward">/$1.$2</to>
            
    </rule>
       <rule>
    <note>
    	将/controller/action 重写为struts定义的url,且包含的参数默认转为id=*
    </note>
            <from>/([a-z_A-Z0-9]+)/([a-z_A-Z0-9]+)/([a-z_A-Z0-9]+)</from>
            <to last="true" type="forward">/$1_$2?id=$3</to>
    </rule>
    <rule>
    <note>
    	将/controller/action 重写为struts定义的url
    </note>
            <from>/([a-z_A-Z0-9]+)/([a-z_A-Z0-9]+)([/]{0,1})</from>
            <to last="true" type="forward">/$1_$2</to>
    </rule>

    <rule>
    <note>
    	将/controller 重写为struts定义的url
    </note>
            <from>/([a-z_A-Z0-9]+)([/]{0,1})</from>
            <to last="true" type="forward">/$1_index</to>
    </rule>
    <rule>
		<note>
			针对首页的请求不过滤
		</note>
		<from>/</from>
		<to last="true" type="forward">/</to>
	</rule>
    <!--
 <rule>
    <note>
    	将/controller/action/id 重写为struts定义的url
    </note>
            <from>/([a-z_A-Z0-9]+)/([a-z_A-Z0-9]+)/([a-z_A-Z0-9]+)</from>
            <to last="true" type="forward">/$1/!$2.action?$1.id=$3</to>
    </rule>
    INSTALLATION

        in your web.xml add...

        <filter>
            <filter-name>UrlRewriteFilter</filter-name>
            <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
            <init-param>
                <param-name>logLevel</param-name>
                <param-value>DEBUG</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>UrlRewriteFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

     EXAMPLES

     Redirect one url
        <rule>
            <from>/some/old/page.html</from>
            <to type="redirect">/very/new/page.html</to>
        </rule>

    Redirect a directory
        <rule>
            <from>/some/olddir/(.*)</from>
            <to type="redirect">/very/newdir/$1</to>
        </rule>

    Clean a url
        <rule>
            <from>/products/([0-9]+)</from>
            <to>/products/index.jsp?product_id=$1</to>
        </rule>
    eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.

    Browser detection
        <rule>
            <condition name="user-agent">Mozilla/[1-4]</condition>
            <from>/some/page.html</from>
            <to>/some/page-for-old-browsers.html</to>
        </rule>
    eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older
    browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.

    Centralised browser detection
        <rule>
            <condition name="user-agent">Mozilla/[1-4]</condition>
            <set type="request" name="browser">moz</set>
        </rule>
    eg, all requests will be checked against the condition and if matched
    request.setAttribute("browser", "moz") will be called.

    -->

</urlrewrite>

 
web.xml中需要配置urlrewrite和struts2,spring。提交代码提示禁用url;代码无法提交。

jsp目录结构

经测试一切正常

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值