首页 > 学院 > 开发设计 > 正文

利用JBuilder 2005 开发Spring实例

2019-11-18 14:08:22
字体:
来源:转载
供稿:网友

  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>
  <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
  10. 设置web模块springapp的属性Content,加入文件log4j.properties,因为classes目录是自动产生的,这一步是为了把文件log4j.properties加到classes目录。

  11. 设置Run Configuration,选菜单Run-Configurations,新建一个Run Configuration,Type选Server,Launch URI设置为:/springapp/hello.htm,给它取名为hello

  12. 运行,你会在messages窗口中看到如下信息:

  資訊: Server startup in 9253 ms

2004-11-05 15:05:00,585 INFO [spring.SpringappController] - <SpringappController - returning hello view>

2004/11/5 下午 03:05:00 org.springframework.web.servlet.view.AbstractCachingViewResolver resolveViewName

  資訊: Cached view 'hello.jsp'

  在myProject/Tomcat/springapp.log文件中增加了一行如下所示:

2004-11-05 15:11:32,348 INFO [spring.SpringappController] - <SpringappController - returning hello view>

  这说明你已成功建立了基本的spring应用。

  jstl的支持,设置工程文件的属性,选菜单Project-Project Properties,将jstl库加入Path/Required Libraries。

  14. 改进前面的例子,改进的文件如下:

  增加“header”文件include.jsp,这是一些jsp文件的公用部分,这样可以使得开发和维护更轻易。所有jsp文件放在web-inf/jsp目录下,为的是只有Controller可以访问View。

  springapp/war/WEB-INF/jsp/include.jsp

<%@ page session="false"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
  利用JSTL,<c:redirect>将页面重定向到Controller,这样就建立了index.jsp和应用架构的联系。

  springapp/war/index.jsp

<%@ include file="/WEB-INF/jsp/include.jsp" %>

<%-- Redirected because we can't set the welcome page to a virtual URL. --%>
<c:redirect url="/hello.htm"/>
  利用JSTL的<c:out>标记,把从Model提取的,传递给View的当前日期和时间作为输出。

  springapp/war/WEB-INF/jsp/hello.jsp

<%@ include file="/WEB-INF/jsp/include.jsp" %>

<html>
<head><title>Hello :: Spring Application</title></head>
<body>
 <h1>Hello - Spring Application</h1>
 <p>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表