當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
day38 19-Spring整合web开发
生活随笔
收集整理的這篇文章主要介紹了
day38 19-Spring整合web开发
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
整合Spring開發環境只需要引入spring-web-3.2.0.RELEASE.jar這個jar包就可以了,因為它已經幫我們做好了.
?
?
?
Spring整合web開發,不用每次都加載Spring環境了。
?
package cn.itcast.service;public class UserService {public void sayHello(){System.out.println("Hello Spring web....."); } } package cn.itcast.servlet;import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;*/ import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import cn.itcast.service.UserService;@SuppressWarnings("serial") public class UserServlet extends HttpServlet { //每次啟動Servlet都會加載Spring的環境.每次運行都需要加載Spring的環境.Spring配置環境中的東西如果多了,每次加載Servlet就加載Spring環境肯定不行.public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/* ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = (UserService) applicationContext.getBean("userService");userService.sayHello();*///得把代碼改了,否則每次都是來獲取Spring的環境.WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());//工具類WebApplicationContextUtilsUserService userService = (UserService) applicationContext.getBean("userService");userService.sayHello();}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request,response);}} <?xml version="1.0" encoding="UTF-8"?> <!-- 別去schema,schema是文件,本地的文件,你得引那個頭 --><beans xmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="userService" class="cn.itcast.service.UserService"></bean> </beans> <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"><display-name></display-name><!-- 配置監聽器ContextLoaderListener --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置全局初始化參數 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>UserServlet</servlet-name><servlet-class>cn.itcast.servlet.UserServlet</servlet-class></servlet><servlet-mapping><servlet-name>UserServlet</servlet-name><url-pattern>/userServlet</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> </web-app>?
轉載于:https://www.cnblogs.com/ZHONGZHENHUA/p/6729544.html
總結
以上是生活随笔為你收集整理的day38 19-Spring整合web开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【视频格式】webm用什么播放
- 下一篇: 13-Flutter移动电商实战-ADB