idea中原生Servlet3.0开发之---使用配置类和注解的方式整合SpringMVC

以注解方式来启动SpringMVC前需了解的基础知识:

1、web容器在启动的时候,会扫描每个jar包下的META-INF/services/javax.servlet.ServletContainerInitializer


2、加载这个文件指定的类SpringServletContainerInitializer


3、spring的应用一启动会加载感兴趣的WebApplicationInitializer接口的下的所有组件;

  
4、并且为WebApplicationInitializer组件创建对象(组件不是接口,不是抽象类)
    1)、AbstractContextLoaderInitializer:创建根容器;createRootApplicationContext();
    2)、AbstractDispatcherServletInitializer:
            创建一个web的ioc容器;createServletApplicationContext();
            创建了DispatcherServlet;createDispatcherServlet();
            将创建的DispatcherServlet添加到ServletContext中;
                getServletMappings();
    3)、AbstractAnnotationConfigDispatcherServletInitializer:注解方式配置的DispatcherServlet初始化器
            创建根容器:createRootApplicationContext()
                    getRootConfigClasses();传入一个配置类
            创建web的ioc容器: createServletApplicationContext();
                    获取配置类;getServletConfigClasses();
===========================    
总结:
    以注解方式来启动SpringMVC;继承AbstractAnnotationConfigDispatcherServletInitializer;
实现抽象方法指定DispatcherServlet的配置信息;

===========================
定制SpringMVC;
1)、@EnableWebMvc:开启SpringMVC定制配置功能;相当于web.xml中配置的 <mvc:annotation-driven/>;

2)、配置组件(视图解析器、视图映射、静态资源映射、拦截器。。。)
    extends WebMvcConfigurerAdapter

===========================

springmvc父子容器

各自负责一些扫描的包

父容器:功能类的包,如:Controller,前端控制器,视图解析器

子容器:业务模块的包,如:service,repositories,数据库事物


使用配置类和注解的方式整合SpringMVC的实例步骤:

一、导入servlet和springmvc的jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.atguigu</groupId>
  <artifactId>springmvc-annotation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-webmvc</artifactId>
  		<version>4.3.11.RELEASE</version>
  	</dependency>
  	
  	<dependency>
  		<groupId>javax.servlet</groupId>
  		<artifactId>servlet-api</artifactId>
  		<version>3.0-alpha-1</version>
  		<scope>provided</scope>
  	</dependency>
  </dependencies>

  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-war-plugin</artifactId>
  			<version>2.4</version>
  			<configuration>
  				<failOnMissingWebXml>false</failOnMissingWebXml>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
</project>

二、创建MyWebAppInitializer .java相当于web.xml文件

并继承抽象类AbstractAnnotationConfigDispatcherServletInitializer(具体可看博文开始时描述)

此类相当于web.xml;加载spring和springmvc,并设置拦截

package com.atguigu;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.atguigu.config.AppConfig;
import com.atguigu.config.RootConfig;

//web容器启动的时候创建对象;调用方法来初始化容器以前前端控制器
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	//获取根容器的配置类;(Spring的配置文件)   父容器;
	@Override
	protected Class<?>[] getRootConfigClasses() {
		// TODO Auto-generated method stub
		return new Class<?>[]{RootConfig.class};
	}

	//获取web容器的配置类(SpringMVC配置文件)  子容器;
	@Override
	protected Class<?>[] getServletConfigClasses() {
		// TODO Auto-generated method stub
		return new Class<?>[]{AppConfig.class};
	}

	//获取DispatcherServlet的映射信息
	//  /:拦截所有请求(包括静态资源(xx.js,xx.png)),但是不包括*.jsp;
	//  /*:拦截所有请求;连*.jsp页面都拦截;jsp页面是tomcat的jsp引擎解析的;
	@Override
	protected String[] getServletMappings() {
		// TODO Auto-generated method stub
		return new String[]{"/"};
	}

}

 三、创建spring的配置文件RootConfig.java,即父容器

package com.atguigu.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

//Spring的容器不扫描controller;父容器
@ComponentScan(value="com.atguigu",excludeFilters={
		@Filter(type=FilterType.ANNOTATION,classes={Controller.class})
})
public class RootConfig {

}

四、创建springmvc的配置文件AppConfig.java,即子容器

package com.atguigu.config;


import java.util.List;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;


//SpringMVC只扫描Controller;子容器
//useDefaultFilters=false 禁用默认的过滤规则;
@ComponentScan(value="com.atguigu",includeFilters={
		@Filter(type=FilterType.ANNOTATION,classes={Controller.class})
},useDefaultFilters=false)
public class AppConfig {


}

五、 框架搭建好后,创建controller和service等业务代码,进行测试

HelloController.java

package com.atguigu.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atguigu.service.HelloService;

@Controller
public class HelloController {
	
	@Autowired
	HelloService helloService;
	
	
	@ResponseBody
	@RequestMapping("/hello")
	public String hello(){
		String hello = helloService.sayHello("tomcat..");
		return hello;
	}

}

HelloService.java

package com.atguigu.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService {
	
	public String sayHello(String name){
		
		return "Hello "+name;
	}

}

 六、运行tomcat测试

说明:controller能访问到,并且能调用service,说明springmvc配置成功

 

 

======以下于你或许是个好消息======

 

好消息就是:欢迎访问下面的博客网站哈哈哈......

 

网站名称:Java学习笔记网 (点击进入)

url:https://www.javaxxbj.com/ (点击进入)

网站特点:

  1. java主要网站的导航目录
  2. 你可以记录自己的博客,并可以控制显示和隐藏,可利于管理啦!!!
  3. 可以添加收藏各个网站的链接!!!
  4. 甚至也可以文章收藏,点赞,关注,查看我的消息等功能哦!!1

看一小点点的截图:

或可一试哦!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
如果你没有使用Maven作为你的项目构建工具,你可以手动配置SpringMVC项目的依赖。以下是配置SpringMVC项目所需的步骤: 1. 下载Spring框架和相关依赖包(包括SpringMVC、Spring Core、Spring Context等)并将它们添加到你的项目。 2. 下载Servlet API和JSP API,并将它们添加到你的项目。 3. 在你的项目创建一个web.xml文件,并添加以下代码: ``` <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>My Spring MVC Application</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` 4. 创建一个dispatcher-servlet.xml文件,并添加以下代码: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="com.example.controller" /> <mvc:annotation-driven /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans> ``` 上述配置文件,我们使用SpringMVC注解驱动方式,并配置了一个视图解析器。 5. 创建一个Controller,例如: ``` package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public ModelAndView hello() { ModelAndView model = new ModelAndView("hello"); model.addObject("message", "Hello World!"); return model; } } ``` 上述代码演示了一个简单的Controller,当访问/hello路径时,会返回一个hello.jsp视图页面,并将一个名为“message”的属性传递给视图。 6. 创建一个hello.jsp视图文件,例如: ``` <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Hello World!</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 上述代码演示了一个简单的JSP页面,它会显示一个名为“message”的属性。 7. 启动你的项目,并通过浏览器访问http://localhost:8080/yourProjectName/hello 来测试你的SpringMVC配置是否正确。 这就是手动配置SpringMVC项目所需的步骤。请注意,这只是一个基本示例,你需要根据你的项目需求进行配置

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值