Spring在Web项目中的使用


对于在web项目中使用Spring,一直有想法,但是没有去实践,今天心血来潮就试了一次。现在讲工程代码放出来供大家学习。

  1. 引用jar包

    • spring-context
    • spring-beans
    • spring-core
    • spring-web
    • spring-aop
    • javax.servlet-api
    • commons-logging

    pom.xml

    <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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.chen</groupId>
    <artifactId>Spring-In-Webapp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Spring-In-Webapp Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies><!--为spring核心提供了大量扩展。可以找到使用spring applicationcontext特性时所需的全部类,jdni所需的全部类,ui方面的用来与模板(templating)引擎如 velocity、freemarker、jasperreports集成的类,以及校验validation方面的相关类,还有ejb,cache,format,jms等等。--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.12.RELEASE</version></dependency><!--springioc(依赖注入)的基础实现,所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行inversion of control / dependency injection(ioc/di)操作相关的所有类。但是这个是个基础实现,一般我们在实际的开发过程中很少直接用到,它是对起到支撑作用的。--><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.3.12.RELEASE</version></dependency><!--spring的核心包,包含spring框架基本的核心工具类,spring其它组件要都要使用到这个包里的类,是其它组件的基本核心。包括asm,cglib以及相关的工具类--><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.12.RELEASE</version></dependency><!--包含web应用开发时,用到spring框架时所需的核心类,包括自动载入webapplicationcontext特性的类、struts与jsf集成类、文件上传的支持类、filter类和大量工具辅助类。--><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.3.12.RELEASE</version></dependency><!-- 包含在应用中使用spring的aop特性时所需的类。 --><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.3.12.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.1</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency>
    </dependencies>
    <build><finalName>Spring-In-Webapp</finalName>
    </build>
    </project>
  2. 项目结构

  3. 代码

    • HelloService.java

          package service;/*** Created by handsome programmer.* User: chen* Date: 2018/1/21* Time: 17:38* Description:*/public class HelloService {public String getUsername(String str) {return "Hello " + str;}}
      
    • HelloSpring.java

      package servlet;import org.springframework.web.context.WebApplicationContext;
      import org.springframework.web.context.support.WebApplicationContextUtils;
      import service.HelloService;import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;/*** Created by handsome programmer.* User: chen* Date: 2018/1/21* Time: 17:24* Description:*/
      @WebServlet(name = "hello", urlPatterns = "/hello")
      public class HelloSpring extends HttpServlet {private HelloService helloService;@Overrideprotected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("UTF-8");String message = request.getParameter("username");//获取Spring容器WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());System.out.println("applicationContext = " + applicationContext);helloService = applicationContext.getBean("helloService", HelloService.class);System.out.println("message = " + message);message = helloService.getUsername(message);request.setAttribute("message", message);request.getRequestDispatcher("index.jsp").forward(request, response);}
      }
      
    • applicationContext.xml\

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="helloService" class="service.HelloService"/>
      </beans>
    • web.xml

      <!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Archetype Created Web Application</display-name><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>
      </web-app>
    • index.jsp

    <%--Created by IntelliJ IDEA.User: chenDate: 2018/1/21Time: 17:44To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head><title>Hehe</title>
    </head>
    <body>
    <h1><%=request.getAttribute("message")%>
    </h1>
    </body>
    </html>
    
    • 访问链接:http://localhost:8080/hello?username=admin
    • 访问效果

    网页

    后台

Spring在web中的使用相关推荐

  1. spring在WEB中的应用。

    1:创建IOC容器.在WEB应用程序启动的时候就创建.利用到监听器. ServletContextListener类的contextInitialized方法中 1 package com.strut ...

  2. 模拟Spring如何在WEB中运行

    Spring在web中配置和普通的Java程序中有所区别,总结一下主要表现在以下几个方面: ①jar包不同,需要引入两个web的jar包 ②需要考虑IOC容器创建的时间 非 WEB 应用在 main ...

  3. odciexttableopen 调用出错 error open log_如何在 Spring 异步调用中传递上下文

    什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步程序执行完即可执行.异步调用指,在程序在执行时,无 ...

  4. delphi 异步 调用 带参数_如何在 Spring 异步调用中传递上下文

    什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步程序执行完即可执行.异步调用指,在程序在执行时,无 ...

  5. Windchill的web中的Spring

    windchill的web.xml文件中有如下代码: <servlet-mapping><servlet-name>MVCDispatcher</servlet-name ...

  6. spring_在基于Spring的Web应用程序中使用Http Session

    spring 在基于Spring的Web应用程序中拥有和使用Http会话有多种方法. 这是基于最近项目经验的总结. 方法1 只需在需要的HttpSession中注入即可. @Service publi ...

  7. 重新学习Spring一--Spring在web项目中的启动过程

    1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...

  8. 如何使用recaptcha_在Spring MVC Web应用程序中使用reCaptcha

    如何使用recaptcha CAPTCHA是一个程序,可以生成人类可以通过但计算机程序" 不能 "通过的测试并对其进行评分. 所采取的策略之一是向用户显示具有扭曲文本的图像,并且用 ...

  9. 在基于Spring的Web应用程序中使用Http Session

    在基于Spring的Web应用程序中拥有和使用Http会话有多种方法. 这是基于最近项目经验的总结. 方法1 只需在需要的HttpSession中注入即可. @Service public class ...

最新文章

  1. 一文深入了解 Redis 内存模型,Redis 的快是有原因的!
  2. 前端开发实习生的第一天
  3. java务必让常量的值在运行期保持不变
  4. 台式计算机总是重启,台式电脑经常自动重启怎么修复
  5. spark基础之RDD详解
  6. BZOJ1101 洛谷3455:[POI2007]ZAP——题解
  7. 我很生气,帮了不足一个月就离开了
  8. 如何让不使用vba没办法打开excel表_Excel常见问题
  9. 称重传感器知识:型号,认证,性能与选择
  10. php字符串中删除字符串函数,PHP实现删除字符串中任何字符的函数
  11. 怎么提高Origin烂橘子的下载速度
  12. 【Web前端】笔试题含解析
  13. JavaScript 读写剪贴板之方式汇总
  14. 我的世界服务器ess配置文件,《我的世界》ess指令大全及用法详解
  15. 鸿蒙王者荣耀想要转区吗,王者荣耀开放转区 转区转角色服务要求条件汇总
  16. 立体视觉相机使用JetsonTX2提升图像
  17. 汽车仪表盘CAN总线实现
  18. 【论文翻译_无数据知识蒸馏_元学习_2022】Up to 100× Faster Data-free Knowledge Distillation
  19. 帕斯卡计算机介绍,帕斯卡计算机:第一台受专利保护的计算机
  20. 苹果笔记本 macbook 阉割后 全格式化后 重新装系统(无mac系统 装win系统解决方案)自己苹果笔记本测试

热门文章

  1. MyBatis 插件原理与自定义插件-PageHelper 原理
  2. MVC 顶层设计-HandlerMapping
  3. Redis高可用方案-公私混合云
  4. SOA实现方式与模式
  5. 封装案例-02-创建(qiang)类
  6. 关闭Eureka的服务自我保护
  7. java 文本 从列开始_如何从sql java中检索文本列?
  8. java addlistener_Java怎样监听文本//比如下面 y.addInputMethodListener((InputMethodListener) this);...
  9. Spring Cloud异常
  10. 计算机与人力资源管理论文,人力资源管理专业计算机能力培养模式论文