SpringMvc环境搭建Demo

新建WEB项目,WEB-INF下创建lib文件夹,导入jar包:

其中的jackson-core和jackson-mapper为@ResponseBody注解的依赖jar包
如果需要连接数据库需要导入数据库驱动包,tomcat的lib目录下也需要导入数据库的jar包
web.xml配置文件:
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:config/spring-*.xml
1
spring
*.do

src目录下新建web.xml配置文件中的config目录,
创建spring-servlet.xml配置文件:
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

注解扫描要注意需要扫描全部使用注解的包,否则error create bean

测试环境:
ApplicationContext context = new ClassPathXmlApplicationContext("/conf/springmvc-servlet.xml");
DemoController controller = (DemoController) context.getBean("demoController");
System.out.println(controller);

你可能感兴趣的:(java)