初学spring:web应用程序中使用Spring
来源:互联网 发布:数据库故障应急预案 编辑:程序博客网 时间:2024/05/01 10:04
简单web应用spring
1、创建Bean
package com.test.spring;public class MessageBean {private String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}}
2、Spring配置:applicationContext.xml(放在WEB-INF/目录下)
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans><!-- id指定bean 的访问名称,class指定bean的实现类 --><bean id="messageBean" class="com.test.spring.MessageBean"><property name="message"><!-- name指定bean实现类的属性 --><value>Spring framework web project test config file:applicationContext.xml!</value><!--指定了向bean属性注入的值 --></property></bean></beans>
注明:spring框架默认使用/WEB-INF/applicationContext.xml作为配置文件。
3、部署描述符文件:web.xml
<?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" id="WebApp_ID" version="2.5">
<display-name>SpringTest</display-name>
<!-- 监听器-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>SpringWebTest</servlet-name>
<servlet-class>com.test.spring.SpringWebTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SpringWebTest</servlet-name>
<url-pattern>/SpringWebTest</url-pattern>
</servlet-mapping>
</web-app>
通过上面的配置,ContextLoaderListener监听器默认查找/WEB-INF/applicationContext.xml文件。
如果要改变spring的配置文件名称或者位置,需要在web.xml中指定,如:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans-config.xml</param-value>
</context-param>
4、创建servlet类:
package com.test.spring;
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.web.context.support.WebApplicationContextUtils;
public class SpringWebTest extends HttpServlet{
/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//super.doGet(req, resp);
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doPost(request, response);
PrintWriter out = response.getWriter();
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
MessageBean bean = (MessageBean)ac.getBean("messageBean");
out.print(bean.getMessage());
out.close();//close
}
}
5、启动tomcat,显示结果。
- 初学spring:web应用程序中使用Spring
- 在web应用程序中使用Spring
- 使用Spring MVC构建Web应用程序
- 使用Spring MVC构建Web应用程序
- spring终结篇 -- 使用spring web应用程序配置
- 【Java.Spring.MVC】使用Spring MVC构建Web应用程序
- 【Spring】在WEB应用中使用Spring
- Spring web 应用程序
- spring web flow初学笔记
- 如何在EJB应用程序中使用Spring
- 在java应用程序中使用spring
- Spring中使用拦截器配置HttpOnly,来提升WEB应用程序的安全性
- spring(5)构建 spring web 应用程序
- spring应用程序使用教程
- Spring Junit4 测试Web应用程序
- Spring Junit4 测试Web应用程序
- Spring Junit4 测试Web应用程序
- Spring Junit4 测试Web应用程序
- Human Gene Functions(最长公共子序列变形题)
- java实现线程的两种方法有什么区别?
- linux下通过JNI用C/C++中调用JAVA类
- 移植QT
- 书籍推荐
- 初学spring:web应用程序中使用Spring
- 移动web app开发必备
- C++语言复习-命名空间
- 策略模式
- Android 4.0 ICS SystemUI浅析——StatusBar加载流程分析
- 国嵌 答疑2
- 如何使用git为开源社区贡献力量
- Windows网络编程基本概念
- switch