Spring 之在Web项目中使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_25034451/article/details/79029479

Spring 在Web中的第一次应用

1、配置Web环境,或者下载有Web插件的eclipse
2、创建Web项目,选择Dynamic Web Project(动态Web项目,对应的静态Web项目 Static Web Project)
3、导入jar包
commons-logging-1.1.1.jar
servlet-api.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
4、创建bean

public class Data {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Data [id=" + id + "]";
    }

}

5、创建Spring配置文件

文件名: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/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="data" class = "com.led.htkj.Data">
        <property name="id" value = "1111111111" />
    </bean> 
</beans>

6、IOC容器在非Web应用中的main方法中直接创建,而在Web应用中,应该在服务器加载时就创建IOC容器

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@WebListener
public class SpringServletContextListener implements ServletContextListener {


    public SpringServletContextListener() {

    }

    public void contextInitialized(ServletContextEvent arg0) {

        ServletContext sct = arg0.getServletContext();
        String config = sct.getInitParameter("configLocation");
        System.out.println(config);
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        sct.setAttribute("ApplicationContext", ctx);
    }

    public void contextDestroyed(ServletContextEvent arg0) {

    }

}

如上代码,实现ServletContextListener 接口,在contextInitialized中创建IOC容器,并保存在ServletContext 中。

然后要在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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置Spring配置文件的名称和位置 -->
  <context-param>
    <param-name>configLocation</param-name>
     <param-value>classpath:ApplicationContext.xml</param-value>
  </context-param>
  <!-- 启动IOC容器的listener -->
  <listener>
    <listener-class>com.led.htkj.servlets.listener.SpringServletContextListener</listener-class>
  </listener>
</web-app>

7、编写servlet

@WebServlet("/CollectingData")
public class CollectingData extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public CollectingData() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext  servletContext = getServletContext();
        ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("ApplicationContext");
        Data data = (Data) ctx.getBean(Data.class);
        System.out.println(data.toString());;
    }
}

8、创建jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href = "CollectingData"> here</a>
</body>
</html>

启动后在页面中点击here结果在控制台输出:

Data [id=1111111111]

总结

错误1:class path resource [applicationContext.xml] cannot be opened because it does not exist
错误2:No qualifying bean of type [com.led.htkj.Data] is defined
都是因为:

`
<context-param>
<param-name>configLocation</param-name>
<param-value>classpath:ApplicationContext.xml</param-value>
</context-param>

中ApplicationContext.xml首字母A被小写,报错误1,百度后在classpath后加了*,结果报错误2,最终发现原来是大字母被小写,解决问题`

猜你喜欢

转载自blog.csdn.net/qq_25034451/article/details/79029479