當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
Spring在web中的使用
生活随笔
收集整理的這篇文章主要介紹了
Spring在web中的使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Spring在Web項(xiàng)目中的使用
對(duì)于在web項(xiàng)目中使用Spring,一直有想法,但是沒有去實(shí)踐,今天心血來潮就試了一次。現(xiàn)在講工程代碼放出來供大家學(xué)習(xí)。
引用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核心提供了大量擴(kuò)展。可以找到使用spring applicationcontext特性時(shí)所需的全部類,jdni所需的全部類,ui方面的用來與模板(templating)引擎如 velocity、freemarker、jasperreports集成的類,以及校驗(yàn)validation方面的相關(guān)類,還有ejb,cache,format,jms等等。--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.12.RELEASE</version></dependency><!--springioc(依賴注入)的基礎(chǔ)實(shí)現(xiàn),所有應(yīng)用都要用到的,它包含訪問配置文件、創(chuàng)建和管理bean以及進(jìn)行inversion of control / dependency injection(ioc/di)操作相關(guān)的所有類。但是這個(gè)是個(gè)基礎(chǔ)實(shí)現(xiàn),一般我們?cè)趯?shí)際的開發(fā)過程中很少直接用到,它是對(duì)起到支撐作用的。--><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.3.12.RELEASE</version></dependency><!--spring的核心包,包含spring框架基本的核心工具類,spring其它組件要都要使用到這個(gè)包里的類,是其它組件的基本核心。包括asm,cglib以及相關(guān)的工具類--><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.12.RELEASE</version></dependency><!--包含web應(yīng)用開發(fā)時(shí),用到spring框架時(shí)所需的核心類,包括自動(dòng)載入webapplicationcontext特性的類、struts與jsf集成類、文件上傳的支持類、filter類和大量工具輔助類。--><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.3.12.RELEASE</version></dependency><!-- 包含在應(yīng)用中使用spring的aop特性時(shí)所需的類。 --><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>項(xiàng)目結(jié)構(gòu)
代碼
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
- 訪問鏈接:http://localhost:8080/hello?username=admin
- 訪問效果
網(wǎng)頁(yè)
后臺(tái)
總結(jié)
以上是生活随笔為你收集整理的Spring在web中的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电话号码分身
- 下一篇: 二叉树的创建和遍历-C语言实现