您的位置:首页 > 编程语言 > Java开发

搭建简单web项目: spring3.0基础配置(1)

2010-10-27 15:47 691 查看
本文为个人学习,记录,交流之用. 请不要转载.

这次从零开始建一个web项目, 发现网上针对spring 3.0的讲解不多, 所以借此机会记录一下配置一个基本的web项目, 主要是配置spring3的过程, 其实大部分和2是类似的.

第一步: 建立并部署一个web项目, (可跳过)
1. 这里想关于web项目简单说一句, 其实一个wen工程最基本的,只看3个地方:
在根目录下(这个目录一般习惯叫WebContext或WebRoot)有
1. WebRoot/WEB-INF/web.xml 启动引导文件
2.WebRoot/WEB-INF/classes/ 编译的class文件,会根据package建立子路径
3.WebRoot/WEB-INF/lib/ jar包 (注意,lib下不可以再建子目录)
这就是一般比较常用的web工程的结构. 有了这三个结构, 然后把WebRoot的绝对路径告诉tomcat这样的web容器,就可以启动了(当然里面要有相应的东西才行).
因此虽然我们一般项目的结构是: projectname/src, projectname/WebRoot 但实际上src里的.java源代码是tomcat完全不关心的,它的解析是从WebRoot开始的, .class才是它认识的.

下面是一个初始的web.xml. 这个文件就相当于项目的启动引导文件, web容器最先找到这个文件,然后解读并一一启动这些类.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

<servlet>
<servlet-name>TestServlet</servlet-name>
<display-name>测试用Servlet</display-name>
<servlet-class>test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


2.部署至tomcat:
tomcat的部署方式有很多, 我比较喜欢的部署方式是apache-tomcat/conf/Catalina/localhost/ 下放入projectname.xml配置文件,
内容为
<Context path="projectname" reloadable="true" docBase="D:/workspace/projectname/WebContent" ></Context>


工程就部署完了. 简单吧.
然后在eclipse里也不用去配什么server,运行环境. 装一个tomcat插件,指定tomcat路径就行了. 上面的xml文件也可以通过配置eclipse-tomcat插件自动生成.

第二步: 把spring的jar包引进来, 这可不是废话哦. 见过spring3.0的都知道, spring3把之前的一个万能spring.jar分拆成了20个小jar. 当然你也可以省事的直接把20个都引进来,没问题.
不过如果你不想这么干, 这里是一个简单能在web中实现最基本的依赖注入所需的jar包:
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
commons-logging-1.1.1.jar
其中commons-logging-1.1.1.jar 是spring的依赖包.

以上是一个不含spring-jdbc数据库管理的合集, 如果要用spring的持久层,还需要:
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
commons-dbcp-1.3.jar
commons.pool-1.5.3.jar
同样,后面2个也是spring的依赖包.
关于spring持久层的配置在下一节介绍

第三步: 配置spring, 基本和spring2还是一样的.
1 在web.xml中加入
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

以前见通过配置spring的Servlet方式, 那种是为了对应在一些低版本的web容器. 这里就不讲了.

2. 在WEB-INF/ 下建一个 spring-conf.xml (名字和路径都随便了, 跟第一步的配置对应就行了.不过出于安全考虑还是不要放到WEB-INF外面.)

<?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-3.0.xsd"> 
<bean id="TestBo"
class="test.TestBo">
</bean>

<bean id="TestAction" class="test.TestAction">
<property name="TestBo" ref="TestBo" />
</bean>
</beans>

这里spring的配置就算完成了, 然后把测试类实现一下, 就可以测测效果啦. 基本除了jar包以外,和spring2都是一致的.

下面是TestServlet , 其他的TestAction和TestBo就随便写了. 启动后访问
http://127.0.0.1:8080/projectname/test/abc 就可以看到执行输出了

package test;

import java.io.IOException;

import javax.servlet.ServletConfig;
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 TestServlet extends HttpServlet {

TestAction ta ;
public TestServlet()
{
}

public void init(ServletConfig servletConfig)
throws ServletException{
servletConfig.getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());
this.ta = ctx.getBean(TestAction.class);
this.ta.sayHello();
System.out.println("TestServlet init");
//     TestAction ta = new TestAction();
//     ta.sayHello();
}

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException
{
doPost(req, resp);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
this.ta.sayHello();
}

public void destroy()
{
}
}


.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: