我是靠谱客的博主 感性大树,最近开发中收集的这篇文章主要介绍Spring MVC 配置XML+注解,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

下面我将阐述两种方法 配置xml和注解 的方法 来使用SpringMVC

1.导入jar包

在这里插入图片描述
2.配置web.xml文件
在这里插入图片描述3.书写配置文件(配置xml方式)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">

	
	<!-- spring MVC 核心配置文件
		1.处理器映射器
		2.处理器配置器
		3.视图解析器
		
	 -->
	
	
	<!-- 处理映射器 -->
		<!-- 1.根据bean的name进行查找Handler 将action的url配置在bean的name中 -->
		<!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />  -->
		
		<!-- 2.简单url映射 -->
		<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
			<property name="mappings">
				<props>
					<!-- /demo1.action /demo2.action  都可以访问到指定的hanler1 -->
					<prop key="/demo1.action">userhandler1</prop>
					<prop key="/demo2.action">userhandler1</prop>
					<prop key="/demo3.action">userhandler2</prop>
					<prop key="/demo4.action">userhandler2</prop>
				</props>
			</property>
		</bean>
		


	<!-- 处理适配器 -->
		<!-- 1.处理器适配器 配置SimpleControllerHandlerAdapter ,要求handler必须实现Controller接口 -->
		<!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />	 -->
		
		
		<!-- 2.处理器适配器 配置HttpRequestHandlerAdapter ,要求handler必须实现HttpRequestHandler接口 -->
		<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
	
	
	
	<!-- 视图解析器 -->
		<!-- ViewResolver -->
		<bean
			class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="viewClass"
				value="org.springframework.web.servlet.view.JstlView" />
	
			<!-- 前缀:/WEB-INF/jsp/ 后缀:.jsp /WEB-INF/jsp/list.jsp -->
			<property name="prefix" value="/WEB-INF/jsp/" />
			<property name="suffix" value=".jsp" />
		</bean>
		
	<!-- 配置bean -->
	
	<bean  name="/demo1.action" id="userhandler1" class="com.baidu.handler.UserHandler1"></bean>
	
	<bean  id="userhandler2" class="com.baidu.handler.UserHandler2"></bean>	
</beans>		

测试的handler类1:

package com.baidu.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class UserHandler1 implements Controller {

	
	
	
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		
		//
		ModelAndView modelAndView = new ModelAndView();
		//传值
		modelAndView.addObject("name", "肥龙");
		//跳转页面
		modelAndView.setViewName("list");
		
		System.out.println("测试请求1.。。。。。。。。。。");
		
		
		return modelAndView;
	}

	
}

测试的handler类2:

package com.baidu.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.ModelAndView;


public class UserHandler2 implements HttpRequestHandler {

	
	
	public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		ModelAndView modelAndView = new ModelAndView();
		
		//response.setCharacterEncoding("utf-8");
		//response.setContentType("application/json;charset=utf-8");
		//response.getWriter().write("json串");

		
		
		System.out.println("请求来了  哈哈哈  这是第二个  ");
		
		
	}

	
	
	

	
}

3.书写配置文件(配置注解方式一)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">

	<!-- 扫描包 -->
		<context:component-scan base-package="com.baidu"></context:component-scan>
	
	
	<!--注解映射器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
		
	<!--注解适配器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
		
	
	<!-- 视图解析器 -->
		<!-- ViewResolver -->
		<bean
			class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="viewClass"
				value="org.springframework.web.servlet.view.JstlView" />
	
			<!-- 前缀:/WEB-INF/jsp/ 后缀:.jsp /WEB-INF/jsp/list.jsp -->
			<property name="prefix" value="/WEB-INF/jsp/" />
			<property name="suffix" value=".jsp" />
		</bean>
		
	
</beans>

测试handler:

package com.baidu.handler;

import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;



@Controller
@RequestMapping(value="/user3")
public class UserHandler3{

	@RequestMapping(value= {"/list.action","/list2.action"})
	public String list() {
		
		System.out.println("请求来了  这是333 使用注解");
		
		
		return "list";
	}

	
	
	

	
}

3.书写配置文件(配置注解方式二)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">

	<!-- 扫描包 -->
		<context:component-scan base-package="com.baidu"></context:component-scan>
	<!-- 牛逼配置 -->
	<mvc:annotation-driven/>
	
	
		
	
	<!-- 视图解析器 -->
		<!-- ViewResolver -->
		<bean
			class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="viewClass"
				value="org.springframework.web.servlet.view.JstlView" />
	
			<!-- 前缀:/WEB-INF/jsp/ 后缀:.jsp /WEB-INF/jsp/list.jsp -->
			<property name="prefix" value="/WEB-INF/jsp/" />
			<property name="suffix" value=".jsp" />
		</bean>
		
	
</beans>	

测试handler:

package com.baidu.handler;

import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;



@Controller
@RequestMapping(value="/user3")
public class UserHandler3{

	@RequestMapping(value= {"/list.action","/list2.action"})
	public String list() {
		
		System.out.println("请求来了  这是333 使用注解");
		
		
		return "list";
	}

	
	
	

	
}

总结:
xml配置 可以修改处理映射器处理适配器来使用 但是 视图解析器不会变

注解 可以使用有两种模式 但是 视图解析器也不会变

最后

以上就是感性大树为你收集整理的Spring MVC 配置XML+注解的全部内容,希望文章能够帮你解决Spring MVC 配置XML+注解所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部