利用JBuilder 2005 开发Spring实例
发表于:2007-06-22来源:作者:点击数:
标签:
下一页 1 2 3 4 1. 下载spring包,网址如下: http://www.springframework.org/download.html 解压后的目录中包含了dist、lib等子目录 2. 在JBuilder2005中增加spring库,选择菜单Tools-Configure-Libraries,在弹出的对话框中点击New按钮,输入spring库的名
下一页 1 2 3 4
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
clearcase/" target="_blank" >cc66 width="90%" align=center bgColor=#e6e4dd border=1>
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> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>springapp</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping>
<welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list>
</web-app> | 8. 在web-inf目录中新建springapp-servlet.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- - Application context definition for "springapp" DispatcherServlet. -->
<beans> <bean id="springappController" class="spring.SpringappController"/> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/hello.htm">springappController</prop> </props> </property> </bean>
</beans> | 9. 在web-inf目录中新建log4j.properties文件如下:
log4j.rootCategory=INFO, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=springapp.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files
log4j.appender.logfile.MaxBackupIndex=3
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
#Pattern to output : date priority [category] - <message>line_separator
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n |
|
原文转自:http://www.ltesting.net