利用JBuilder 2005开发Spring实例
时间:2010-12-04
1. 下载spring包,网址如下:
http://www.springframework.org/download.html
解压后的目录中包含了dist、lib等子目录
2. 在JBuilder2005中增加spring库,选择菜单Tools-Configure-Libraries,在弹出的对话框中点击New按钮,输入spring库的名称:spring,点击Add按钮,将dist目录中的所有jar文件增加到spring库中
3. 新建工程文件,选菜单File-New Project,给工程文件取名为myProject
设置工程文件的属性,选菜单Project-Project Properties,选择Tomcat作为服务器,将spring库加入Path/Required Libraries。因为例子中用到了log4j,将包含了log4j的库加入Path/Required Libraries,注意到lib目录下有两个子目录log4j和jakarta-commons,它们的用法是不同的,如下面的代码片断所示:
log4j
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */
static Logger logger = Logger.getLogger(SpringappController.class);
jakarta-commons
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
4. 新建Web模块,选菜单File-New,给Web模块取名为springapp
5. 新建hello.jsp文件如下:
<%@ page contentType="text/html; charset=Big5" %>
<html>
<head><title>Example :: Spring Application</title></head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings.</p>
</body>
</html>
6. 新建类文件SpringappController.java如下:
package spring;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */
// protected final Log logger = LogFactory.getLog(getClass());
static Logger logger = Logger.getLogger(SpringappController.class);
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info("SpringappController - returning hello view");
return new ModelAndView("hello.jsp");
}
}
7. 修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|