web-j2EE整合(二)

1.为web项目添加Spring

1.将spring有关联的包添加大项目中:

spring.jar

cglib.jar

jakarta-commons我文件夹下的jar ( commons-attributes-compiler.jar  删除)

log4j.jar

2.创建applicationContext.xml 文件(还没有找到该DTD 文件的位置)

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

</beans>

项目启动成功

3.测试spring

创建一个bean Person

 

在spring配置文件中配置 将 name属性注入

 

创建一个beanTestSpring

将person 注入到 TestSpring

 

写一个测试类 Servlet

 

 

public class SpringServletTest extends HttpServlet {

 

protected void service(HttpServletRequest arg0, HttpServletResponse resp)

throws ServletException, IOException {

// TODO Auto-generated method stub

 

ApplicationContext ac =  new ClassPathXmlApplicationContext("applicationContext.xml");

Person p =(Person)ac.getBean("person");

p.info();

 

TestSpring ts = (TestSpring)ac.getBean("testSpring");

ts.test();

resp.setCharacterEncoding("UTF-8");

resp.setContentType("test/html");

PrintWriter pw = resp.getWriter();

pw.println("请切换到控制台,查看执行结果.........");

}

 

}

 

在web.xml中配置servlet测试

<servlet>

<servlet-name>testSpring</servlet-name>

<servlet-class>com.younglibin.www.lb.test.SpringServletTest</servlet-class>

</servlet>

 

<servlet-mapping>

<servlet-name>testSpring</servlet-name>

<url-pattern>/spring/test</url-pattern>

</servlet-mapping>

 

启动项目,地址栏输入:

http://localhost:8080/上下文/spring/test

可以在bean职工输出一些东西在控制台

4.在web中配置Spring

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

为了便于项目文件管理将spring配置文件放在classes目录下的spring中,在这里指定文件路径

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:spring/applicationContext.xml</param-value>

</context-param>

修改测试类中获得spring的方式:

package com.younglibin.www.lb.test;

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletContext;

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;

 

import com.younglibin.www.lb.Person;

import com.younglibin.www.lb.TestSpring;

 

 

public class SpringServletTest extends HttpServlet {

 

protected void service(HttpServletRequest arg0, HttpServletResponse resp)

throws ServletException, IOException {

// TODO Auto-generated method stub

ServletContext sc = this.getServletContext();

ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);

//ApplicationContext ac =  new ClassPathXmlApplicationContext("applicationContext.xml");

Person p =(Person)ac.getBean("person");

p.info();

 

TestSpring ts = (TestSpring)ac.getBean("testSpring");

ts.test();

resp.setCharacterEncoding("UTF-8");

resp.setContentType("test/html");

PrintWriter pw = resp.getWriter();

pw.println("请切换到控制台,查看执行结果.........");

}

 

}

 

 

启动tomcat 测试;

 

 

5.用spring管理数据源

参考spring中 “用spring控制JNDI数据源链接”

6.在spring文件中配置了bean 在struts配置中找不到action

1.在工程添加strust2-spring-plugin-*.jar

2.在struts.properties 中 struts.objectFactory = spring  可用

 

 

 

 

 

 

你可能感兴趣的:(J2EE)