springMVC 多 dispatcherServlet 配置

本文介绍如何在Spring MVC中配置多个DispatcherServlet以实现URL分发,避免注解冲突,并详细展示了web.xml中的配置方法及JSP页面的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                  springMVC 注解流行使用,也就面临一个问题,如果我们的Xml配置文件只有一个,那么在多人共同开发时,大家都不知道其他人注解的名称,

就会注解重复,或者我们需要根据业务不同,我们需要进行不同处理,这是  就需要配置多个 dispatcherServlet ,对不同的URL进行分发。

                配置 如下

                  web.xml

<!--************* spring 主xml 文件配置 *************-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/classes/applicationContext.xml
		</param-value>
	</context-param>
	<!--**************  ************** -->
	<servlet>
		<servlet-name>common</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet>
		<servlet-name>rest</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet>
		<servlet-name>validate</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>common</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>rest</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>validate</servlet-name>
		<url-pattern>/jm/validate/*</url-pattern>

    我们先配置 主xml  ,spring  公用的Bean ,包括数据库连接/数据源配置 ,事务配置 等,然后是分xml,我们 可以根据开发人员配置,或根据 业务配置,

所有的分xml  均可以访问主xml  上的bean

     spring 默认xml 文件命名为     xxx-serlvet.xml,存放在  web-inf  下,所以 我们需要创建 分 xml 文件  common-servlet.xml,rest-servlet.xml,validate-servlet.xml

jsp页面 如图

 

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <script type="text/javascript">
  function show(){
  		var aac001=document.getElementById("aac001").value; 
  		var url= "<%=path%>/person/showPerson.do?aac001="+aac001;
  		var form = document.f1;
  		
  		form.action =url;
  		alert(url);
  		form.submit();
  }
  function xs(){
  		var aac001=document.getElementById("aac001").value; 
  		var url= "<%=path%>/jm/validate/login/"+aac001;
  		var form = document.f1;
  		form.method="get";
  		form.action =url;
  		alert(url);
  		form.submit();
  }
  </script>
  <body>
   		<form method="post" id="f1" name="f1"  action="person/showPerson.do">
   		<input type="text" id="aac001">
   		<input type="button"  value="hibernate测试"  onclick="show()">
   		<input type="button"  value="hibernate测试2222"  onclick="xs()">
   		</form>
   		<a href="<%=path%>/rest/showPower">权限显示</a>
   		<a href="<%=path%>/rest/login">测试111</a>
   		<a href="<%=path%>/jm/validate/xs">测试23232</a>
  </body>
</html>


 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值