朗志工作室(Langzhi Studio)

江浙沪一带找工作中,欢迎联系

  博客园 :: 首页 :: 联系 :: 订阅 订阅 :: 管理
  3461 Posts :: 2 Stories :: 497 Comments :: 7 Trackbacks

公告

本站大量内容为转载,一般都保留原链接,如果侵犯了你的权益,请以各种方式联系我,我会第一时间修正。

将我自己会忘记的内容,觉得好的,想法备在此处, 所以比较凌乱,莫怪
 


QQ:1036130199


msn/GTalk: Frederick.mao@gmail.com
twitter:http://twitter.com/mlzboy



新浪微博 http://weibo.com/mlzboy
profile 2011
移动充话费
我的简历2011.9

我的简历2010.7
项目截图


分享

抓虾
pageflakes
Rojo
google reader
netvibes
my yahoo
newsgator
bloglines
鲜果
哪吒
有道
QQ邮箱
抓虾
pageflakes
Rojo
google reader
netvibes
my yahoo
newsgator
bloglines
鲜果
哪吒
有道
QQ邮箱
分享家:Addthis中文版
昵称:lexus
园龄:4年10个月
粉丝:21
关注:5

搜索

 

常用链接

我的标签

最新评论

阅读排行榜

评论排行榜

推荐排行榜

2012年2月12日 #

brasero

posted @ 2012-02-12 10:14 lexus 阅读(2) 评论(0) 编辑

Ubuntu 制作光盘镜像文件

出自Guoshuang Wiki

从光盘建立 iso 文件

sudo umount /dev/cdrom dd if=/dev/cdrom of=file.iso bs=1024

从目录建立 iso 文件

mkisofs -r -o file.iso /location_of_folder/

做 md5 验证

md5sum file.iso > file.iso.md5
posted @ 2012-02-12 10:06 lexus 阅读(5) 评论(0) 编辑

Mybatis:代码生成器(Maven版)&集成Spri

 

http://tywo45.iteye.com/blog/1207581

ng

posted @ 2012-02-12 01:01 lexus 阅读(8) 评论(0) 编辑

。。。。

posted @ 2012-02-12 00:52 lexus 阅读(14) 评论(0) 编辑

2012年2月11日 #

www.cdnzz.com/plan

posted @ 2012-02-11 23:40 lexus 阅读(7) 评论(0) 编辑

http://www.sysprobs.com/

posted @ 2012-02-11 14:14 lexus 阅读(5) 评论(0) 编辑

Physical to Virtual Machine by VMware vCenter Converter

If you are trying to convert physical tovirtual machine and to be used in VMware workstation or Sun VirtualBox, then continue reading this article. VMware vCenter converter is being used to convert physical to virtual (P2V) to VMware ESX server. But you can use this converter to create VMDK and VMX files locally.

When you want to convert physical tovirtual machine and use locally in VMware workstation or Sun VirtualBox, this method will help you lot.

Physical to Virtual

Follow the steps to create VMX and VMDK files by converting physical to virtual (P2V) in VMware vCenter Converter.

1)  Download VMware vCenter Convert for Physical to Virtual (it’s completely free- registration required)

2)  Complete the installation.

3)  Open the converter, select ‘This local machine’. You can select remote computer also. You must have administrator privileges on remote computer to achieve this.

Physical to Virtual

Click on View Source details to make sure converter is reading your physical computer resources correctly.

4) Click next. In destination type select VMware workstation and choose your VMware product version where you will be running this virtual machine.

Physical to Virtual

Type a name for new virtual machine. If you will be connecting this physical machine and new virtual machine by networking, better to give separate name. Select the location for virtual machine. Make sure the location is having enough of disk space. It depends on your physical computer disk sizes and what disks you select to convert.

5) Click Next, this screen brings some more advanced option. Click on ‘Data to copy’ to choose the physical partitions to be included in conversion. Obviously, you can’t remove system or boot partitions from the list which will cause virtual machine booting problems. Target disks size in virtual machine is editable in this screen.

convert3

6) Other options such as processors, disk types, memory size, network type and even running windows services also can be changed.
Similar options are available when you convert by VMware workstation converter which is mentioned here.

7) Click Next and check the summery list as per your selection. Press Finish to start Physical to Virtual machine conversion by VMware vCenter Converter. Let the conversion complete. Status can be viewed as below.

Physical to Virtual

8)  Unfortunately I didn’t allow completing due to space problem. But I did the same live physical to virtual conversation by VMware workstation converter, and opened virtual machine successfully.Read here for more information.

9) Once conversation is over, open the virtual machine in VMware workstation or VMware player. That’s it. Enjoy.

Recommended Posts

posted @ 2012-02-11 14:13 lexus 阅读(2) 评论(0) 编辑

Is it possible to convert a Physical Ubuntu Install (Intel based PC with Ubuntu 10.10) to a virtual box disk, so that I can use that same environment on a separate pc or can keep it virtual for testing?

Something similar to "Disk2VHD" which converts a physical Windows 2K3 and above box into a VHD or VMware image.

Will be grateful for your assistance ! Kind Regards

linkimprove this question

25% accept rate
feedback

Yes, however it will most likely take a very long time. I haven't actually tried the steps below, but they should work.

First, you need to make an image of your entire partition. You need to know the partition path of the Ubuntu partition, for example /dev/sda1 would be the first partition on the first attached hard drive.

sudo dd if=PART_PATH of=OUTPUT_PATH/ubuntu.bin

note that you should do the above command from a liveCD with the partition you are trying to copy unmounted -- i.e. you need to have another hard drive or something to copy all the data to.

You can shut the liveCD down and boot back into Ubuntu to perform the last step:

Convert the binary into a vbox drive:

VBoxManage convertdd PATH_TO_ubuntu.bin ubuntu.vdi --format VDI

you can then use the resulting ubuntu.vdi as a Virtualbox drive. Just make sure you have plenty of time and hard drive space for this operation. A little tip: commandline tasks can be suspended by hitting The operation will go to sleep, and you can use you computer again. When you are interested in resuming the process, type fg in the terminal, and the operation will resume. Quite handy for long operations like this.

posted @ 2012-02-11 14:05 lexus 阅读(3) 评论(0) 编辑

P2V of a Physical Machine with vCenter Converter

P2V is a large subject to cover, as I cannot cover every scenario, so I am going to briefly cover a P2V conversion of an physical Ubuntu server I have kicking around. Many companies are trying to reduce there footprint and convert many physical machines into virtual ones, web servers, SMTP server are ideal candidates. The conversion is a two step process, the actual conversion and the cleanup process after you have created the VM. There are a number of 3rd party P2V converters including free ones but I will be using VMware own vCenter Converter to perform this task. Vware vCenter converter is a windows based application, that installs onto the vCenter management server, it fully supports both windows and linux conversions but you may want to check VMwareown website for all the available supported OS's. There are two versions of the converter available

  • Starter - is agent based and is free
  • Enterprise - can be either be agent or can boot from a cd

VMware vCenter converter is not a silver bullet, you may have to work with the converted VM to get it working properly, also if the server originally had problems those problems may be replicated in the VM environment. The more complex the physical server (could be a domain control, cluster server, etc), the more you may have problems converting it and getting it to work, here is where you need to look at the VMware forums or search the internet for answers.

Installing vCenter Converter

Installing the vCenter converter is like a normal application installation, on the vCenter server start the installation by putting in the vCenter 4.1 cd, and then selecting vCenter Converter

During the installation process, you will be asked about the vCenter management server details

Once the installation has finished select the plugins -> manage-plugins from the top bar of the vCenter window, then select download and install plugin

That's it, you are now ready to convert physical servers

P2V

Once you have the P2V plugin installed we can now import a physical server, I have a old HP D510 desktop PC with Ubuntu 32-bit OS running on it (yep old pc and old OS), lets convert this physical machine into a virtual machine. First select the ESXi server that you wish to import the new virtual machine into, in my case I have selected vmware01, right-click and select "import machine"

Now enter the details of the running physical server, I selected "view source details", just to make sure the connection is working and we have access to the physical server

All looks good, it see's my physical server as a Ubuntu 8.04 32-bit server

The next screen, gives us details on where the VM will be placed, I changed the datastore to a shared storage area (filer2_ds1), you can change the name of the VM if you wish

The screen allows us to configure the VM's hardware

I notice that the network was in correct, so selected edit and changed the network from private to public and can add additional NIC's if you wish

The "data to copy" screen allows us to select what volumes/filesystems we wish to copy across, there may be a case that you do not some volumes/filesystems copied and thus speed up the importing process.

With very little effort we are at the summary screen ready to import our physical server

It took about 5 minutes to import this server, the time it takes all depends on how much data you need to import, you can monitor the progress from the "recent tasks" window in the vCenter man screen

And here we have it, a fully imported physical server, I double checked the hardware settings and it done a pretty good job as I did not have to change anything

When I started the VM, all I had to do was edit the /etc/network/interfaces file and change the NIC to eth3 and up the interface and away we go, as you can see below

Ok not all P2V conversions will go as smoothly as this one did but you get the idea on what is involved and what you need to prepare before hand when performing a P2V conversion. There is also a Cold-Clone boot CD which allows you reboot the physical server from the CD and clone the physical server while it is offline.

posted @ 2012-02-11 14:01 lexus 阅读(4) 评论(0) 编辑

Ubuntu 10.04 Desktop 快速添加微软雅黑字体

向Ubuntu里添加雅黑字体,貌似已经成为了中文用户的一项必要功课。

即使你像我一样喜欢使用英文的系统,还是需要在浏览网页,编辑文档,收发邮件等过程中用到中文字体。

如果你Google或者参考 Ubuntu Wiki 就很容易找到向 Ubuntu 里添加字体的方法。

一直以来我也是这么做的,但是最近我发现 Ubuntu 10.04 以后有了更简单的方法。

你只需要三步便可完成:

1. 从Windows的机器上复制雅黑字体文件。以Win7为例,在 %windir%\fonts 下面找到雅黑字体,默认有两个文件(字体)

2. 复制到Ubuntu的机器上,双击字体文件选择 Install Font (安装字体),或者在右键列表里选择安装

3. 现在字体已经可以使用了,你直接在你想要设置的程序里修改就可以了

 

另外设置Firefox的中文字体有一个小技巧。

你不应该去设置默认字体,这样会更改英文的字体。

你应该去高级里设置中文Chinese的字体以及其他Other的字体。

Other对应于Unicode。

这样可以保持英文字体不变,中文字体改为雅黑。

posted @ 2012-02-11 12:43 lexus 阅读(5) 评论(0) 编辑

Ubuntu/linux下最强大的下载工具-aria2

aria2是我今天新学到的一个命令行的下载工具,虽然还没研究透,但是他绝对比wget更好.

 

aria2 是 Linux 下一个不错的高速下载工具。由于它具有分段下载引擎,所以支持从多个地址或者从一 个地址的多个连接来下载同一个文件。这样自然就大大加快了文件的下载速 度。aria2 也具有断点续传功能,这使你随时能够恢复已经中断的文件下载。除了支持一般的 http(s) 和 ftp 协议外,aria2 还支持 BitTorrent 协议。这意味着,你也可以使用 aria2 来下载 torrent 文件。

 

安装 aria2
 
aria2 目前已被包含到许多 Linux 发行版中,因此你可以通过所用的系统直接加以安装。例如,在 Debian/Ubuntu 中,你可以在终端执行如下指令来安装 aria2:

 

sudo apt-get install aria2

 

aria2 的使用方法

 

aria2 是命令行程序,使用非常简单。

 

  • 一般使用使用 aria2 下载文件,只需在命令后附加地址即可。比如我们下载ubuntu如:
    aria2c http://www.mirror.tw/pub/ubuntu/releases/jaunty/ubuntu-9.04-desktop-i386.iso
  • 分段下载利用 aria2 的分段下载功能可以加快文件的下载速度,对于下载大文件时特别有用。为了使用 aria2 的分段下载功能,你需要在命令中指定 s 选项。如:
    aria2c -s 2 http://www.mirror.tw/pub/ubuntu/releases/jaunty/ubuntu-9.04-desktop-i386.iso

    这将使用 2 个连接来下载该文件。s 后面的参数值介于 1~5 之间,你可以根据实际情况选择。

  • 断点续传在命令中使用 c 选项可以断点续传文件。如:
    aria2c -c http://www.mirror.tw/pub/ubuntu/releases/jaunty/ubuntu-9.04-desktop-i386.iso
  • 下载 torrent 文件你也可以使用 aria2 下载 BitTorrent 文件。如:
    aria2c -o gutsy.torrent http://cdimage.ubuntu.com/daily-live/current/gutsy-desktop-i386.iso.torrent

关于 aria2 的更多用法,可以通过 man aria2c 查阅。

posted @ 2012-02-11 07:51 lexus 阅读(10) 评论(0) 编辑

Home > virtualization > How To Convert VMWare Image (.vmdk) to VirtualBox Image (.vdi)

How To Convert VMWare Image (.vmdk) to VirtualBox Image (.vdi)

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

I’ve found more and more people are switching to Virtualbox these days and I keep running into the same question.  ”How can I convert my VMware images to Virtualbox images?”  Well, breath easy because it is possible and not very difficult!

There are two methods that I am aware of that will allow you to use your VMware images on Virtualbox.  The first method I’ll outline uses the graphical Virtualbox interface and the second uses the command line. Each method should be equally as stable, it simple depends on your preference.

Method 1

Start Virtualbox
Inside the Virtualbox Menu click: File > Virtual Media Manager
Click on the “Add” button.
Locate and select the VMware .vmdk file you’d like to convert.
Click Open.
Verify your disk image has been added to the list of images and that the virtual and actual sizes appear accurate.
Click OK
Create a new virtual machine profile, selecting your imported image for the storage.
Boot your new virtual machine.

Method 2
To use the second method we’ll need an addition command-line tool called qemu.

sudo aptitude install qemu

You’ll new be able to convert a .vmdk (VMware image) to a .bin format, which can then be converted to a Virtualbox native .vdi format.

qemu-img convert /path/to/original.vmdk converted.bin

You’ll then need to use the VBoxManage utility that comes with Virtualbox to convert the .bin to a native .vdi format:

VBoxManage convertdd converted.bin converted.vdi

You can now create your new Virtualbox machine profile, using this new .vdi file as your disk image.

Two methods for converting VMware images to Virtualbox images.  Are there any other methods that you can suggest, or have you had better experience with one or the other?  Let us know!

  1. Kragil
    March 22nd, 2009 at 08:41 | #1
posted @ 2012-02-11 06:45 lexus 阅读(5) 评论(0) 编辑

VMware vCenter Converter Standalone

Automate and simplify physical to virtual machine conversions as well as conversions between virtual machine formats with Converter Standalone.

Features & Benefits

VMware vCenter Converter Standalone can be run on a wide variety of hardware and supports most commonly used versions of Microsoft Windows and Linux operating systems. Quickly and reliably convert local and remote physical machines into virtual machines without any disruption or downtime.

Complete multiple conversions simultaneously with a centralized management console and an intuitive conversion wizard.

DOWNLOAD INCLUDES
  • VMware vCenter Converter Standalone

Learn more about VMware vCenter Converter Standalone
Evaluate VMware vSphere

posted @ 2012-02-11 06:35 lexus 阅读(5) 评论(0) 编辑

VBoxManage modifyhd xpvm.vdi --resize 20000
du -sh vmxp.vdi
ref:
posted @ 2012-02-11 00:52 lexus 阅读(6) 评论(0) 编辑

 

Java: Good way to encapsulate Integer.parseInt()

http://stackoverflow.com/questions/1486077/java-good-way-to-encapsulate-integer-parseint#

 

 

 

 

java.toString() ,(String),valueOf

 (2009-11-14 23:56:43)
标签: 

it

 
在java项目的实际开发和应用中,常常需要用到将对象转为String这一基本功能。本文将对常用的转换方法进行一个总结。常用的方法有Object#toString(),(String)要转换的对象,String.valueOf(Object)等。下面对这些方法一一进行分析。
方法1:采用 Object#toString()方法请看下面的例子:
Object object = getObject();
System.out.println(object.toString());
在这种使用方法中,因为java.lang.Object类里已有public方法.toString(),所以对任何严格意义上的java对象都可以调用此方法。但在使用时要注意,必须保证object不是null值,否则将抛出NullPointerException异常。采用这种方法时,通常派生类会覆盖Object里的toString()方法。
方法2:采用类型转换(String)object方法这是标准的类型转换,将object转成String类型的值。使用这种方法时,需要注意的是类型必须能转成String类型。因此最好用instanceof做个类型检查,以判断是否可以转换。否则容易抛出CalssCastException异常。此外,需特别小心的是因定义为Object 类型的对象在转成String时语法检查并不会报错,这将可能导致潜在的错误存在。这时要格外小心。如:
Object obj = new Integer(100);
String strVal = (String)obj;
在运行时将会出错,因为将Integer类型强制转换为String类型,无法通过。但是,
Integer obj = new Integer(100);
String strVal = (String)obj;
如是格式代码,将会报语法错误。
此外,因null值可以强制转换为任何java类类型,(String)null也是合法的。
方法3:采用String.valueOf(Object) String.valueOf(Object)的基础是Object#toString()。但它与Object#toString()又有所不同。在前面方法1的分析中提到,使用后者时需保证不为null。但采用第三种方法时,将不用担心object是否为null值这一问题。为了便于说明问题,我们来分析一下相关的源代码。Jdk里String# valueOf(Object)源码如下:

public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString(); }
从上面的源码可以很清晰的看出null值不用担心的理由。但是,这也恰恰给了我们隐患。我们应当注意到,当object为null时,String.valueOf(object)的值是字符串”null”,而不是null!!!在使用过程中切记要注意。试想一下,如果我们用 if(String.valueOf(object)==null){System.out.println(“传入的值是null!”);}这样的语句将可能会发生什么问题。再想一下,向控制台输出时,在视觉上如下语句在执行的结果上有什么不同:
System.out.println(String.valueOf(null));
System.out.println(null);
我们看到的输出将是一模一样的东西:null,但它们意义相同吗?
以上是对object对象转换为String的一些总结。
Convert a String to an int, returning zero if the conversion fails.
 
    
   
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 
/**
 
 *
 @author John Keyes (john at integralsource.com)
 @version $Revision: 680644 $, $Date: 2008-07-29 01:13:48 -0700 (Tue, 29 Jul 2008) $
 */
public class Main {
 
 
  //--------------------------------------------------------------------
  
  /**
   * <p>Convert a <code>String</code> to an <code>int</code>, returning
   * <code>zero</code> if the conversion fails.</p>
   
   @param str  the string to convert
   @return the int represented by the string, or <code>zero</code> if
   *  conversion fails
   */
  public static int stringToInt(String str) {
      return stringToInt(str, 0);
  }
 
  /**
   * <p>Convert a <code>String</code> to an <code>int</code>, returning a
   * default value if the conversion fails.</p>
   
   @param str  the string to convert
   @param defaultValue  the default value
   @return the int represented by the string, or the default if conversion fails
   */
  public static int stringToInt(String str, int defaultValue) {
      try {
          return Integer.parseInt(str);
      catch (NumberFormatException nfe) {
          return defaultValue;
      }
  }
 
}
 
   

http://www.java2s.com/Code/Java/Data-Type/ConvertaStringtoanintreturningzeroiftheconversionfails.htm

 

 

posted @ 2012-02-11 00:37 lexus 阅读(4) 评论(0) 编辑

2012年2月10日 #

DBeaver 1.5 正式版发布了! 

DBeaver 是一个通用的数据库管理工具和 SQL 客户端,支持 MySQL、PostgreSQL、Oracle、DB2、MSSQL、Sybase、Mimer、HSQLDB、Derby以及其他兼容 JDBC 的数据库。DBeaver 提供一个图形界面用来查看数据库结构、执行SQL查询和脚本、浏览和导出数据、处理BLOB/CLOB 数据以及修改数据库结构等。 


该版本的新特性包括: 

  • 新增WMI(Windows管理规范,这是一项核心的Windows管理技术)浏览器
  • 重新设计了系统架构,以支持非关系型数据库
  • 改进了ERD插件
  • 修复了Oracle插件附件,提升了性能
  • 修复了大量的bug

posted @ 2012-02-10 23:54 lexus 阅读(3) 评论(0) 编辑

backbones.js

knockout

http://www.iteye.com/topic/1120223?page=2

posted @ 2012-02-10 23:50 lexus 阅读(3) 评论(0) 编辑

摘要: 您还未登录 !登录注册论坛首页→Java企业应用论坛→Mybatis3.0查询,保存,更新,删除数据。全部HibernateSpringStrutsiBATIS企业应用LuceneSOAJava综合设计模式TomcatOOJBoss最成熟稳定甘特图控件,支持Java和.Net浏览 337 次主题:Mybatis3.0查询,保存,更新,删除数据。精华帖 (0) :: 良好帖 (0) :: 新手帖 (...阅读全文
posted @ 2012-02-10 23:44 lexus 阅读(3) 评论(0) 编辑

      这次的项目中,一个同事遇到了一个奇怪的问题。背景是这样的,项目中使用了struts2+spring,在前端有一个功能实现是使用JQuery的 ajax请求访问一个struts2的action,要求返回的是view是json。其中项目加入了如下本问题相关的jar包:struts2- core-2.1.8.1.jar,struts2-json-plugin-2.2.1.jar,json-lib-2.4-jdk15.jar。具体 的代码与配置都没有问题,即使用了标准的相关写法,因为不涉及到这个问题的讨论,所以就不贴代码了。因为当时帮他调试的时候没有记录具体的异常,我在网上 找到了相关的问题贴,转载如下:http://hi.baidu.com/%BF%AA%CB%B3/blog/item/c06cbf451d530b8cb3b7dc4c.html

异常形式:

      Class org.apache.struts2.json.JSONWriter can not access a member of * 或是 Class com.googlecode.jsonplugin.JSONWriter can not access a member of class* 第一种是struct2.1.8与json结合时的异常,第二种是struct2.1.6与json结合的异常。

具体:

      Class org.apache.struts2.json.JSONWriter can not access a member of class oracle.jdbc.driver.BaseResultSet with modifiers "public"

解释:

      不能把程序中的某种数据结构串行化成json格式。

原因:

      struts2的action里面的数据转换成json数据时,会将提供了get方法的属性都串行化输出JSON到客户端。有的时候,很多属性并不能串行 化成json数据,比如这里的oracle.jdbc.driver.BaseResultSet。这时还进行强行转换就会出现这样的异常。

解决方法:

      在不能串行化到json的属性相应的get方法前加一条json标记@JSON(serialize=false)。告诉json不需要转化这个属性。或者根本不写这个get方法。

后记:

      对于不需要在前台输出的json数据,也可以用同样的方法进行处理,从而减少服务器和客户端间交互的信息量。
      可在需要在前台输出的属性的get方法前加上@JSON(name="status")标识,从而为该属性起了一个别名,在前台就可以通过status作为属性名来读取其值。


      由此可见是因为序列化问题造成的,反思我们项目中的问题,原因是因为在action中注入service时使用的是接口(网上的另一种说法是在 action中的接口不能给予get方法,但是那只是描述了现象,如果上文所述正确的话那从本质上说明了原因),而且提供了get方法,接口不能被串行 化。去掉get方法后,异常消失。先在此记录下,有空了看下相关的源代码,拿源码说事更有说服力.... 
posted @ 2012-02-10 01:05 lexus 阅读(16) 评论(0) 编辑

感觉现在的JAVA框架越来越多,纷繁复杂。想彻底研究一种框架,了解下其中的一些通用的东西,于是选择了struts2。

看了很多struts2的文档,发现很多都是在讲struts2与JSP使用很多标签库,我不喜欢用那么多的标签,于是研究了下如何使用velocity与 struts2整合。

 

主要有四个步骤:

 

1,添加pom依赖 (这里我采用 maven管理依赖,相当方便)

Xml代码  收藏代码
  1. <dependency>  
  2.     <groupId>org.apache.velocity</groupId>  
  3.     <artifactId>velocity</artifactId>  
  4.     <version>1.6</version>  
  5.     <scope>compile</scope>  
  6. </dependency>  
  7. <dependency>  
  8.     <groupId>org.apache.velocity</groupId>  
  9.     <artifactId>velocity-tools</artifactId>  
  10.     <version>1.3</version>  
  11.     <scope>compile</scope>  
  12. </dependency>  

2,修改web.xml 

Xml代码  收藏代码
  1. <!DOCTYPE web-app PUBLIC  
  2.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  3.  "http://java.sun.com/dtd/web-app_2_3.dtd" >  
  4.   
  5. <web-app>  
  6.     <display-name>Archetype Created Web Application</display-name>  
  7.   
  8.     <filter>  
  9.         <filter-name>struts2</filter-name>  
  10.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  11.     </filter>  
  12.     <filter-mapping>  
  13.         <filter-name>struts2</filter-name>  
  14.         <url-pattern>/*</url-pattern>  
  15.     </filter-mapping>  
  16.   
  17.     <servlet>  
  18.         <servlet-name>velocity</servlet-name>  
  19.         <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>  
  20.     </servlet>  
  21.   
  22.     <!-- Map *.vm files to Velocity -->  
  23.     <servlet-mapping>  
  24.         <servlet-name>velocity</servlet-name>  
  25.         <url-pattern>*.vm</url-pattern>  
  26.     </servlet-mapping>  
  27.   
  28.     <welcome-file-list>  
  29.         <welcome-file>index.vm<welcome-file>  
  30.     </welcome-file-list>  
  31. </web-app>  

 

3,增加/WEB-INF/velocity.properties

这里根据需要加velocity参数,我暂时啥参数也不加,整个空文件,用默认的总可以吧,需要的时候再加好了。

 

4,增加/EB-INF/toolbox.xml

Xml代码  收藏代码
  1. <?xml version="1.0"?>  
  2.   
  3. <toolbox>  
  4.     <tool>  
  5.         <key>link</key>  
  6.         <scope>request</scope>  
  7.         <class>org.apache.velocity.tools.struts.StrutsLinkTool</class>  
  8.     </tool>  
  9.     <tool>  
  10.         <key>msg</key>  
  11.         <scope>request</scope>  
  12.         <class>org.apache.velocity.tools.struts.MessageTool</class>  
  13.     </tool>  
  14.     <tool>  
  15.         <key>errors</key>  
  16.         <scope>request</scope>  
  17.         <class>org.apache.velocity.tools.struts.ErrorsTool</class>  
  18.     </tool>  
  19.     <tool>  
  20.         <key>form</key>  
  21.         <scope>request</scope>  
  22.         <class>org.apache.velocity.tools.struts.FormTool</class>  
  23.     </tool>  
  24.     <tool>  
  25.         <key>tiles</key>  
  26.         <scope>request</scope>  
  27.         <class>org.apache.velocity.tools.struts.TilesTool</class>  
  28.     </tool>  
  29.     <tool>  
  30.         <key>validator</key>  
  31.         <scope>request</scope>  
  32.         <class>org.apache.velocity.tools.struts.ValidatorTool</class>  
  33.     </tool>  
  34. </toolbox>  

 

好,到此为止整合完毕。

 

最后添上一个helloworld.vm

Html代码  收藏代码
  1. velocity的helloWorld  
  2. #foreach( $header in $request.HeaderNames )  
  3.   <b>$header:</b> $request.getHeader($header)<br>  
  4. #end  

 

试一下吧,会打出浏览器的一些请求的head出来。原来不是helloworld,呵呵。但是还是小有成就感,至少出来点东西了吗。

 

====================邪恶的分割线==================

 

接下来我们来真正的helloWorld。

 

1,建立index.vm

Html代码  收藏代码
  1. <html>  
  2. <body>  
  3. <form action="HelloWorld">  
  4.     <input type="text" name="userName"/>  
  5.     <input type="submit" value="提交" />  
  6. </form>  
  7. </body>  
  8. </html>  

 

 2,修改struts.xml

加上如下配置

Html代码  收藏代码
  1. <package name="default" extends="struts-default">  
  2.     <action name="HelloWorld" class="com.yajun.helloworld.HelloWorld">  
  3.         <result name="SUCCESS">/success.vm</result>  
  4.     </action>  
  5. </package>  

 3,如上面的配置所示,还需要加入

com.yajun.helloworld.HelloWorld 这个类:

Java代码  收藏代码
  1. package com.yajun.helloworld;  
  2.   
  3. public class HelloWorld {  
  4.   
  5.     private String message;  
  6.     private String userName;  
  7.   
  8.     public HelloWorld(){  
  9.     }  
  10.   
  11.     public String execute() {  
  12.         setMessage("Hello " + getUserName());  
  13.         return "SUCCESS";  
  14.     }  
  15.   
  16.     public String getMessage() {  
  17.         return message;  
  18.     }  
  19.   
  20.     public void setMessage(String message) {  
  21.         this.message = message;  
  22.     }  
  23.   
  24.     public String getUserName() {  
  25.         return userName;  
  26.     }  
  27.   
  28.     public void setUserName(String userName) {  
  29.         this.userName = userName;  
  30.     }  
  31.   
  32. }  

 

和success.vm

Html代码  收藏代码
  1. <html>  
  2. <head>  
  3. <title>Hello World</title>  
  4. </head>  
  5. <body>  
  6. <h1>$message</h1>  
  7. </body>  
  8. </html>  

 

完成了,运行吧。哈哈。

 

感觉比标签清爽许多,而且velocity可以直接从 struts2 的 valueStack中 取得像要的值,还是挺不错的。

 

但是,有突然发现,中文显示有问题呀。于是想到了velocity.properties那个文件还需要配置个东西:

 

input.encoding = GBK
output.encoding = GBK

 

这下一个HelloWorld应该OK了。

posted @ 2012-02-10 01:02 lexus 阅读(14) 评论(0) 编辑