Spring项目添加javeweb框架
项目加入tomcat容器
下面这种情况没有项目的Artifact
参照
https://blog.csdn.net/tj867182298/article/details/83308483
设置web主界面
web.xml中
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
其中index.html放在web目录下,与WEB-INF同级
创建服务器类
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
UserService userService = app.getBean(UserService.class);
userService.save();
}
}
web.xml中添加服务器和路由映射
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.CakeCN.web.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
创建全局的Spring容器:在ServletContext域中,通过监听服务器开启这个事件创建
创建一个新类继承ServletContextListener
这个类实现的两个方法contextInitialized,contextDestroyed
分别是监听ServletContext创建和关闭的两个事件。我们在创建事件中实现Spring容器创建
package com.CakeCN.listener;
import com.CakeCN.config.SpringConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextLoaderListener implements ServletContextListener {
//服务器一启动,创建上下文(Spring容器创建)
public void contextInitialized(ServletContextEvent servletContextEvent) {
ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
//上下文存储到ServletContext域中
ServletContext servletContext = servletContextEvent.getServletContext();
servletContext.setAttribute("app",app);
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
web.xml
中注册一下监听器
在业务层直接从ServletContext中获取Spring
<listener>
<listener-class>com.CakeCN.listener.ContextLoaderListener</listener-class>
</listener>
package com.CakeCN.web;
import com.CakeCN.config.SpringConfiguration;
import com.CakeCN.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
ApplicationContext app = (ApplicationContext)servletContext.getAttribute("app");
UserService userService = app.getBean(UserService.class);
userService.save();
}
}
Spring-Web包集成了上条功能,只需要在web.xml中配置ContextLoaderListener
后即可通过WebApplicationContextUtils
获取到Spring容器
pom.xml中添加上对应版本的spring-web
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
web.xml中配置ContextLoaderListener
contextConfigLocation
用来指定applicationContext.xml的路径,放在resources路径下,故为classpath,这样contextConfigLocation
才能找到这个文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
业务层调用
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
//ApplicationContext app = (ApplicationContext)servletContext.getAttribute("app");
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserService userService = app.getBean(UserService.class);
userService.save();
}
}
SpringMVC配置
pom.xml导包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
web.xml配置Spring-MVCDispatcherServlet
顾名思义调度服务器,所有请求都会发送到这个服务器,然后分派出给到各自的处理方法中
<!-- 配置Spring-MVC的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 任何请求都走这个servlet-->
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
使用注解的方式创建Spring容器中,处理指定路由的服务器对象的处理方法
package com.CakeCN.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.xml.ws.RequestWrapper;
@Controller //标记是一群处理方法切片类
public class UserController {
@RequestMapping("/save")//路由为save
public String save(){
System.out.println("Controller save running...");
return "success.jsp";//这里直接能返回jsp视图名字,Spring容器直接就能解析
}
}
使用了注解,在Spinrg-mvc的Spring容器中要标记一下组件扫描
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Controller 的组件扫描-->
<context:component-scan base-package="com.CakeCN.controller"/>
</beans>
而这个Spring-MVC的Spring容器配置xml文件要在web.xml中注册时就指定,参照前面的代码“ 配置Spring-MVC的前端控制器 ”
这里由于所有请求都会走MVC的调度服务器,所以主页面<welcome-file>index.html</welcome-file>
的跳转失败了,会404,是正常现象。