SpringMVC___使用xml以及注解简单配置

它是一个动态的web项目
一、xml配置的形式
applicationContext.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<!-- 注册对应的 处理器映射器 -->
	 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> 
	<!-- 注册对应的 处理器适配器 -->
	 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
	 
	<!-- 注册自定义的Controller,并加上路径:/hello,相当于注解的@RequestMapping("/hello") -->
	 <bean class="com.controller.MyController" name="/hello"></bean>  
	</beans>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringDemo08</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
	  <servlet-name>spring-mvc</servlet-name>
	  <!-- 中央处理器or前端控制器 -->
	  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	  <init-param>
  		<param-name>contextConfigLocation</param-name>
  		<!-- 配置文件 -->
  		<param-value>classpath:applicationContext.xml</param-value>
  	   </init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>spring-mvc</servlet-name>
  	<!-- 映射路径 -->
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

Controller 在不使用注解时要实现接口
org.springframework.web.servlet.mvc.Controller

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class MyController implements Controller{
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("----返回ModelAndView----");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/index.jsp");
		return mav;
	}
	
}

二、使用注解
applicationContext.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<!-- 开启扫描 -->
	<context:component-scan base-package="com.controller"/>
	<!-- 开启SpringMVC注解的方式 -->
	<mvc:annotation-driven/>
</beans>

Web.xml 这里基本上没有变化

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringDemo08</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
	  <servlet-name>spring-mvc</servlet-name>
	  <!-- 中央处理器or前端控制器 -->
	  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	  <init-param>
  		<param-name>contextConfigLocation</param-name>
  		<!-- 配置文件 -->
  		<param-value>classpath:applicationContext.xml</param-value>
  	   </init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>spring-mvc</servlet-name>
  	<!-- 映射路径 -->
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

Controller这里不需要实现Controller接口,只要加上注解就可以把它交给spring容器管理

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
/*@RequestMapping("/con") //这里也可以配置访问这整一个Controller路径*/
public class MyController{
	
	@RequestMapping("/sel")
	public ModelAndView selIndex() {
		// TODO Auto-generated method stub
		System.out.println("----返回ModelAndView----");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/index.jsp");
		return mav;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷偷学习被我发现

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值