• 2006-10-08

    ideabook 采用GPLv2发布

    Views: 21756 | No Comments

    ideabook 采用GPLv2发布, 因为我不愿意自己再写一个许可证, 而且也没有必要. 以前没使用, 是因为我没有时间阅读GPL.

    Posted by ideawu at 2006-10-08 20:01:49
  • 2006-09-30

    为什么总有那么多的人喜欢吃那么不好吃的东西!

    Views: 10929 | 1 Comment

    好久没有写日志了, 无论是技术日志还是心情日志或者是"看不惯"的日志.

    学习Java Web编程, 发现越学越不如开始的时候好玩了. 以前就把JSP, PHP当成是网页嵌入式语言, 做起东西来觉得挺有成就感. 不过, 最近发现自己成了代码工人, 当然是我自己的工人.

    不过对一句话有了体会: 程序 = 算法 + 数据结构.

    还有, 时间太宝贵了. 没有不能做的事, 只有做事的时间不够. 当然, 你不能因为这句话而自以为是, 这只是说那些合理的事.

    前几天我的左眼的上眼皮突然长了一个小包, 害我不停的眨眼. 估计明天或者后天就能好. 是我最近吃油炸的东西太多了吗? 那些该死的混蛋, 他们做的是什么食物, 于是我不得不去吃油炸食物. 为什么总有那么多的人喜欢吃那么不好吃的东西!

    Posted by ideawu at 2006-09-30 16:35:03
  • 2006-09-19

    The Day You Went Away — M2M

    Views: 14489 | 3 Comments

    M2M 的 The Day You Went Away 是我最喜欢的一首歌. 我第一次听到是在一个网吧. 吧台的服务员也很喜欢这首歌, 我去的每一次都能听到她放. 有一次, 我趁没人在吧台的电脑旁边就过去看歌曲的名字.

    你可以下载到这首歌的伴奏, 伴奏有时比歌曲还要好听. 伴奏在 http://www.banzou.cn/ 下载.

    Well I wonder could it be
    When I was dreaming about you baby
    You were dreaming of me
    Call me crazy
    Call me blind
    To still be suffering is stupid after all of this time
    
    Did I lose my love to someone better
    And does she love you like I do
    I do, you know I really really do
    
    Well hey
    So much I need to say
    Been lonely since the day
    The day you went away
    
    So sad but true
    For me there's only you
    Been crying since the day
    the day you went away
    
    I remember date and time
    September twenty second
    Sunday twenty five after nine
    In the doorway with your case
    No longer shouting at each other
    There were tears on our faces
    
    And we were letting go of something special
    Something we'll never have again
    I know, I guess I really really know
    
    Well hey
    So much I need to say
    Been lonely since the day
    The day you went away
    
    So sad but true
    For me there's only you
    Been crying since the day
    the day you went away
    
    Why do we never know what we've got till it's gone
    How could I carry on the day you went away
    Cause I've been missing you so much I have to say
    
    Posted by ideawu at 2006-09-19 16:03:29
  • 2006-09-18

    9.18 忽忘国耻!

    Views: 8553 | No Comments

    9.18 忽忘国耻!

    9月18日晚10时许, 日本关东军借口铁路被中国军队破坏, 向中国东北军驻地北大营发动进攻. 从此, 日本开始了长达14年的侵华战争, 之间犯下了"南京大屠杀"等罪行. 至今, 日本官方和民间的主流都没有反醒侵略战争的罪恶.

    在中国日军死亡45万人左右, 占日军二战总死亡数的20%左右. 而中国3500万人伤亡.

    Posted by ideawu at 2006-09-18 12:31:36
  • 2006-09-06

    ideabook 新版本发布

    Views: 21884 | No Comments

    这个版本的 ideabook 相对前一个 jsp + javabean 版本性能提高了. 当然, 结构似乎有点复杂.

    一些特性:

    1. 基于 Spring 框架.
    2. 使用数据库连接池.
    3. 使用 jstl, 所以 jsp 代码更加简洁.
    4. 增加 access 层, 实现最新留言的缓存, 从而减少数据库连接次数, 提高性能.

    访问 ideabook 首页

    Posted by ideawu at 2006-09-06 19:33:34
  • 2006-08-26

    Spring MVC 入门

    Views: 26556 | 21 Comments

    这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦!

    首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用程序接收到 http://localhost:8080/hello.do(事实上请求路径是 /hello.do) 的请求后, Spring 将这个请求交给一个名为 helloController 的程序进行处理, helloController 再调用 一个名为 hello.jsp 的 jsp 文件生成 HTML 代码发给用户的浏览器显示. 上面的名称(/hello.do, helloController, hello.jsp) 都是变量, 你可以更改.

    在 Spring MVC 中, jsp 文件中尽量不要有 Java 代码, 只有 HTML 代码和"迭代(forEach)"与"判断(if)"两个jstl标签. jsp 文件只作为渲染(或称为视图 View)模板使用.

    好了, 我们开始吧. 首先我们需要一个放在 WEB-INF 目录下的 web.xml 文件:

    web.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 
     3 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
     4          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
     6          http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
     7 
     8     <context-param>
     9         <param-name>contextConfigLocation</param-name>
    10         <param-value>
    11             /WEB-INF/database.xml
    12             /WEB-INF/applicationContext.xml
    13         </param-value>
    14     </context-param>
    15 
    16     <listener>
    17         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    18     </listener>
    19 
    20     <filter>
    21         <filter-name>encodingFilter</filter-name>
    22         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    23         <init-param>
    24             <param-name>encoding</param-name>
    25             <param-value>UTF-8</param-value>
    26         </init-param>
    27     </filter>
    28 
    29     <filter-mapping>
    30         <filter-name>encodingFilter</filter-name>
    31         <url-pattern>*.do</url-pattern>
    32     </filter-mapping>
    33 
    34     <servlet>
    35         <servlet-name>ideawu</servlet-name>
    36         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    37         <load-on-startup>1</load-on-startup>
    38     </servlet>
    39 
    40     <servlet-mapping>
    41         <servlet-name>ideawu</servlet-name>
    42         <url-pattern>*.do</url-pattern>
    43     </servlet-mapping>
    44 
    45     <welcome-file-list>
    46         <welcome-file>index.jsp</welcome-file>
    47         <welcome-file>index.html</welcome-file>
    48     </welcome-file-list>
    49 
    50     <jsp-config>
    51         <taglib>
    52             <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
    53             <taglib-location>/WEB-INF/tld/c.tld</taglib-location>
    54         </taglib>
    55         <taglib>
    56             <taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri>
    57             <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location>
    58         </taglib>
    59     </jsp-config>
    60 
    61 </web-app> 

    它配置了以下功能:

    • 配置 DispatcherServlet (servlet 标签), 它是一个 Java Servlet 程序. 我们将它命名为 ideawu. 然后我们再配置 Servlet 映射(servlet-mapping 标签), 也就是你希望哪些请求被DispatcherServlet处理. 这里, 我们设置后缀名为 do(*.do) 的所有URL请求都被名为 ideawu 的 DispatcherServlet 的程序处理. 选择 .do 只是一个习惯,但是你不要选择 .html! 虽然《Spring in Action》选择了 .html, 但是那是一种非常糟糕的作法, 特别是你整合 ApacheTomcat 的时候.

    • 配置 CharacterEncodingFilter (filter 标签), 否则你会发现中文乱码. 因为我的 jsp 和 html 文件都是 UTF-8 编码的, 所以我在 param-value 标签中设置了 UTF-8. 估计你使用的是 GB2312 或者 GBK, 立即转到 UTF-8 上来吧.

    • 分解配置文件. context-param 标签指明我们的配置文件还有 /WEB-INF/database.xml 和 /WEB-INF/applicationContext.xml. ContextLoaderListener(listener 标签) 由此得知配置文件是哪些, 它会将它们载入.

    因为我们将 DispatcherServlet 命名为 ideawu, 所以我们在 WEB-INF 目录下建立一个名为 ideawu-servlet.xml 的文件:

    ideawu-servlet.xml:

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
     3 
     4 <beans>
     5 
     6     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     7         <property name="prefix" value="/WEB-INF/jsp/" />
     8         <property name="suffix" value=".jsp" />
     9     </bean>
    10 
    11     <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    12         <property name="mappings">
    13             <props>
    14                 <prop key="/hello.do">helloController</prop>
    15             </props>
    16         </property>
    17     </bean>
    18 
    19     <bean id="helloController" class="com.ideawu.HelloController">
    20         <!--
    21         <property name="helloManager" ref="helloManager" />
    22         -->
    23     </bean>
    24 
    25 </beans> 

    它配置了以下功能:

    • 配置 InternalResourceViewResolver, 它是 jsp 渲染模板的处理器. 如果你告诉 InternalResourceViewResolver 处理一个名为 hello 的模板时, 它会渲染 /WEB-INF/jsp/hello.jsp 文件. 把 jsp 文件放到 /WEB-INF/jsp/ 目录下是被鼓励的, 这样可以防止用户不经过 Controller 直接访问 jsp 文件从而出错(有些顽皮的人很喜欢这样做).

    • 配置 SimpleUrlHandlerMapping, 在上面的配置文件中, /hello.do 的请求将被 helloController 处理. "/hello.do"和"helloController" 是变量, 你可以更改. 但是你注意到了吗, hello.do 以 .do 作为后缀名. 如果这里(本文的条件下)你 不使用.do 作为后缀名, 就没有程序来处理这个请求了. 因为 DispatcherServlet 将收到的请求转交给 SimpleUrlHandlerMapping, DispatcherServlet 收不到的请求, SimpleUrlHandlerMapping 当然也收不到了. 你可以在 props 标签内配置多个 prop 标签.

    • 我们将在后面编写 com.ideawu.HelloController 类.

    上面, 我们在 web.xml 文件中告诉 ContextLoaderListener, 我们还有另外两个配置文件 /WEB-INF/database.xml 和 /WEB-INF/applicationContext.xml.

    applicationContext.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
     3 
     4 <beans>
     5 
     6     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     7         <property name="locations">
     8             <list>
     9                 <value>/WEB-INF/jdbc.properties</value>
    10             </list>
    11         </property>
    12     </bean>
    13 
    14 </beans> 

    它配置了以下功能:

    • 读取 /WEB-INF/jdbc.properties 文件. 你可以在 list 标签中配置多个 value 标签.

    database.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
     3 
     4 <beans>
     5 
     6     <!-- Remove this if your database setting is fine.
     7     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
     8         <property name="driverClassName" value="${jdbc.driverClassName}"/>
     9         <property name="url" value="${jdbc.url}"/>
    10         <property name="username" value="${jdbc.username}"/>
    11         <property name="password" value="${jdbc.password}"/>
    12     </bean>
    13     -->
    14 
    15     <!-- Transaction manager for a single JDBC DataSource
    16     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    17         <property name="dataSource" ref="dataSource"/>
    18     </bean>
    19     -->
    20 
    21     <!--
    22     <bean id="attributeManager" class="com.ideawu.core.AttributeManager">
    23         <property name="dataSource" ref="dataSource"/>
    24     </bean>
    25     -->
    26 
    27 </beans> 

    它配置了以下功能(不过,已经注释掉了):

    • 配置数据库连接. 类似${jbbc.url}是一种访问变量的方法. 我们可以从 /WEB-INF/jdbc.properties 中找到这个变量的值. 如果你的数据库已经配置好, 就将第一个注释去掉.

    jdbc.properties:

    1 jdbc.driverClassName=com.mysql.jdbc.Driver
    2 jdbc.url=jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8
    3 jdbc.username=test
    4 jdbc.password=12345 

    现在, 我们来编写 Java 代码吧.

     1 /***********************************************************
     2 * Date: 2006-8-26
     3 * File: HelloController.java
     4 * Author: ideawu
     5 ***********************************************************/
     6 
     7 package com.ideawu;
     8 
     9 import org.springframework.web.servlet.mvc.Controller;
    10 import org.springframework.web.servlet.ModelAndView;
    11 
    12 import javax.servlet.http.HttpServletRequest;
    13 import javax.servlet.http.HttpServletResponse;
    14 
    15 /**
    16  * @author ideawu
    17  *
    18  */
    19 public class HelloController implements Controller {
    20 /*  
    21     private HelloManager helloManager;
    22 
    23     public void setHelloManager(HelloManager helloManager) {
    24         this.helloManager = helloManager;
    25     }
    26 */
    27 
    28     public ModelAndView handleRequest(HttpServletRequest request,
    29             HttpServletResponse response)throws Exception{
    30 
    31         request.setAttribute("hello_1", "你好啊, Spring!");
    32         request.setAttribute("hello_2", "Hello World!");
    33 
    34         return new ModelAndView("hello");
    35     }
    36 
    37 }
    

    return new ModelAndView("hello"); 告诉 InternalResourceViewResolver jsp 模板的名字叫作 hello. request.setAttribute() 设置的对象我们可以在 jsp 文件中使用.

    hello.jsp:

     1 <%@ page contentType="text/html; charset=UTF-8" %>
     2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     4 <html xmlns="http://www.w3.org/1999/xhtml">
     5 <head>
     6     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     7     <title>Hello World!</title>
     8 </head>
     9 <body>
    10 
    11 <h2>${hello_1}</h2>
    12 
    13 <h2>${hello_2}</h2>
    14 
    15 </body>
    16 </html> 

    你可以下载整个 Web 应用程序. 在 Debian Linux, Tomcat 5.5.16, JDK1.5.0 下运行良好. 解压后得到一个 spring 文件夹, 放到你的 webapps 目录下, 在浏览器中输入 http://localhost:8080/spring/hello.do 就可以访问了.

    Posted by ideawu at 2006-08-26 10:17:43
|<<<112113114115116117118119120>>>| 116/138 Pages, 825 Results.