创建maven的web项目

这个项目src下没有test等文件——手动创建

 
关于web-app version="3.0" 的问题
如何改成推荐使用的web-app 4.0?

再添加 就是默认4.0版本的了
配置监听器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--配置一个监听器: 监听Tomcat启动
      帮我们创建Spring容器
      spring-web的依赖中
   -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--指定spring配置文件或者是配置类
        默认值: WEB-INF/applicationContext.xml
        使用context-param 指定
     -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
</web-app>Spring与web整合
service为空的原因:
一个项目有两个容器:tomcat容器、Spring容器
访问的servlet是tomcat启动的
@Autowire()servlet注入了service 但是servlet没有注入到spring容器里 servlet没有交给spring管理
@Controller:把这个类交给spring但是servlet不能交给Spring容器
无法获取spring容器中的service 只能手动getBean()注入service
手动getBean()
//提供获取Spring容器的工具类 WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(application);  //手动获取bean userService = webApplicationContext.getBean(UserService.class); User user = userService.queryById(Integer.parseInt(id)); //json格式发送 String json = JSON.toJSONString(user); response.setContentType("application/json;charset=UTF-8"); PrintWriter writer = response.getWriter(); writer.print(json); writer.flush();
![[附源码]JAVA毕业设计线上导医系统(系统+LW)](https://img-blog.csdnimg.cn/2a10e38a875643f6a3e4df8047dad027.png)







![[附源码]JAVA毕业设计无人驾驶汽车管理系统(系统+LW)](https://img-blog.csdnimg.cn/23e25721c8c444b8992cdebd1ef97301.png)
![[附源码]JAVA毕业设计西藏民族大学论文管理系统(系统+LW)](https://img-blog.csdnimg.cn/acdcaa5435ab44de8f85230231af9c86.png)








