水仁博客

上善若水,仁恕载物
随笔 - 11, 文章 - 0, 评论 - 4, 引用 - 0
数据加载中……

2008年6月7日

SpringMVC 2.5 的HelloWorld

首先,写一个Bean

package springmvc.one.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/hellOne.act")
public class HelloOneAction { 

@RequestMapping
public String handleRequest(String user,Model model) { 
System.out.println("用户名:"+user); //GET/POST的入参
model.addAttribute("user", user); //通过Session返回到界面的出参
model.addAttribute("helloWord", "Hello");

return "hellouser";

}


再写一个JSP页面hellouser.jsp,此页面放在 WEB-INF/jsp 目录下,代码如下:

<html> 
<head><title>HelloPage</title></head> 
<body> 
Test this sample!
<H1> ${helloWord}, ${user}!</H2> 
</body>
</html>


接着看看web.xml的配置

<?xml version="1.0" encoding="ISO-8859-1"?> 

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<description>Spring 2.5 App</description> 
<display-name>Spring App Examples</display-name> 

<servlet> 
<servlet-name>annomvc</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup> 
</servlet> 

<servlet-mapping> 
<servlet-name>annomvc</servlet-name> 
<url-pattern>*.act</url-pattern> 
</servlet-mapping> 

</web-app>


最后就是,annomvc.xml 文件了。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="springmvc.one.web"/>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

</beans>


好了,见一个Tomcat工程,在 tomcat 中运行,访问下面连接,就可以运行了。

http://localhost:8080/TOMCAT-PROJECT/hellOne.act?user=gjhuai

posted @ 2008-09-27 14:31 水仁圭 阅读(3465) | 评论 (1)编辑 收藏

[专业]JS一些知识-0806081757


JS的日期加减函数:

function dateAdd(date,dayNum){
var a = date.valueOf();
a = a + dayNum * 24 * 60 * 60 * 1000;
a = new Date(a);
return a;
}


JS把字符串装载到DOM对象:
var doc = new ActiveXObject("MSxml2.DOMDocument");
doc.loadXML( xmlStr);


当JS的正则表达式的pattern需要动态构造时,需要使用RegExp类:
patt=new RegExp(today.replace(/\-/g,"\\-")+"((.|\n|\r|\t)+)"+yesterday.replace(/\-/g,"\\-"),"gm");
//patt=new RegExp("2008\-5\-26((.|\n)+)2008\-5\-25","gm");
h = h.replace(patt,yesterday);
而不能使用/dd/gm之类简单的pattern




posted @ 2008-06-08 17:58 水仁圭 阅读(220) | 评论 (0)编辑 收藏

[专业]Python读gbk编码的xml问题-0806072220


Python读xml时,如果编码不是utf-8或utf-16,就出错,如下:


...

解析这个xml文件代码如下:
from xml.dom import minidom
f = minidom.parse('f:\\temp\\protocol.xml')
print f.toxml()

出现这个错误:
xml.parsers.expat.ExpatError: unknown encoding:


解决办法:

由于xml协会规定,所有xml解析器均需要支持utf-8和utf-16两种编码而不要求别的编码,所以我估计python提供的xml处理模块就是不支持gb2312的。而windows下的文件,大部分均为gb2312编码的,因此处理的时候,就会带来不方便的地方。

解1:利用UltraEdit等工具,将xml文件转换成UTF-8的,然后encoding="utf-8"即可
转换工具如果没有,用python可以简单写一个,比如
(以下代码转自 http://tenyears.cn/?cat=6 )
----------
# -*- coding: mbcs -*-
import codecs
f = codecs.open(‘D:\\normal.txt’, ‘rb’, ‘mbcs’)
text = f.read().encode(‘utf-8′)
f.close
f = open(‘d:\\utf8.txt’, ‘wb’)
f.write(text)
f.close()
print text.decode(‘utf-8′).encode(‘gb2312′)
-----------------

解2:xml文件里面不要写入encoding,保持为gb2312本地编码,然后程序解析的时候,采用语句
unicode(file('f:\\temp\\a.txt', 'r', 'gb2312').read(),'gb2312').encode('utf-8')
将整个文件转成utf-8的 String 来处理,处理结束后,利用
unicode(string,'utf-8').encode('gb2312')
换成本地的gb码,再将结果写回文件。

另外,python2.4的普通函数处理字符串的时候,好像已经支持各种编码了。


posted @ 2008-06-07 22:22 水仁圭 阅读(3153) | 评论 (0)编辑 收藏

[专业]代码阅读的经验-0806072109


由于工作上的原因,我不得不看大量别人写的代码,这是一件很痛苦的事,尤其是看既少文档注释,又无良好命名和结构的代码.

有本书叫Code Reading,中文译作代码阅读方法与实践, 简单浏览了一遍电子文档, 感觉还是隔靴搔痒, 对提高代码阅读效率并无太大的帮助. 自己感觉还是以下方法有些帮助:
1. 把对代码阅读的认识用笔或wiki记下来, 最好根据功能结构分类,可画些辅助理解的框图或思维导图
2. 利用UML工具反向生成些类图,包图, 还可自己动手画一些流程图,时序图和协作图
3. 利用调试工具,通过设断点,单步调试,设观察哨等手段看看到底它是怎么运行的
4. 写一些简单的测试程序,通过断言,日志来验证自己的判断
5. 如有可能,和代码的原作者或其他维护者一起做Code Review


posted @ 2008-06-07 21:11 水仁圭 阅读(228) | 评论 (0)编辑 收藏