水仁博客

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

2007年12月30日

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)编辑 收藏

Spring 集成测试

8.3.1. Context管理和缓存


Spring 中的包 spring-mock.jar 为集成测试提供了一流的支持。所有相关的API在包 org.springframework.test 中,它们不依赖于任何应用服务器或者其他部署环境。

test包里的各种抽象类提供了如下的功能:
  • 各测试案例执行期间的Spring IoC容器缓存。
  • 测试fixture自身的依赖注入。
  • 适合集成测试的事务管理。
  • 继承而来的对测试有用的各种实例变量。

test包对加载的Context提供缓存,缓存功能是通过 AbstractDependencyInjectionSpringContextTests 类的一个方法(如下)实现的,此方法提供contexts xml的位置,且实现这个方法的类必须提供一个包含XML配置文件位置数组。缺省情况下,一旦加载后,这些配置将被所有的测试案例重用。

protected abstract String[] getConfigLocations();

当配置环境受到破坏,AbstractDependencyInjectionSpringContextTests 的 setDirty() 方法可以用来重新加载配置,并在执行下一个测试案例前重建application context。

8.3.2. 测试fixture的依赖注入

AbstractDependencyInjectionSpringContextTests 类将从getConfigLocations()方法指定的配置文件中自动查找你要测试的类, 并通过Setter注入该类的实例。

简单例子

public class HibernateTitleDaoTests extends AbstractDependencyInjectionSpringContextTests {

// 被测试类的实例将被(自动的)依赖注入
private HibernateTitleDao titleDao;

// 一个用来实现'titleDao'实例变量依赖注入的setter方法
public void setTitleDao (HibernateTitleDao titleDao) {
this.titleDao = titleDao;
}

public void testLoadTitle() throws Exception {
Title title = this.titleDao.loadTitle(new Long10));
assertNotNull(title);
}

//指定Spring配置文件加载这个fixture
protected String[] getConfigLocations() {
return new String[] { "classpath:com/foo/daos.xml" };
}

}

getConfigLocations() 使用的是 根据类型的自动装配(autowire byType)来注入的,所以如果你有多个bean都定义为一个类型,则对这些bean你不能用这个方法。在这种情况下你要使用 applicationContext 实例变量,并且使用 getBean() 来进行显式查找。

如果你的测试案例不使用依赖注入,只要不定义任何setters方法即可; 或者你可以继承 org.springframework.test.AbstractSpringContextTests 类,它只包括用来加载Spring Context的便利方法,并且在测试fixture中不进行依赖注入。

8.3.3. 事务管理

测试对持久存储的数据会有改动。类 AbstractTransactionalDataSourceSpringContextTests 在缺省情况下,对每一个测试案例,他们创建并且回滚一个事务。所以使用这个类就不用担心数据被修改;它依赖于Application Context中定义的一个bean PlatformTransactionManager。

如果你确实想在测试时修改数据,可以用这个类的 setComplete() 方法,这将提交而不是回滚事务。另外, endTransaction() 方法可以在测试结束前中止事务。

8.3.4. 方便的变量

AbstractDependencyInjectionSpringContextTests 类提供了两个保护属性性实例变量:

  • applicationContext (a ConfigurableApplicationContext): 可以利用它进行显式bean查找,或者作为一个整体来测试这个Context的状态。
  • jdbcTemplate : 对确定数据状态的查询很有用。

8.3.5. 示例

Spring的PetClinic实例展示了这些测试超类的用法

public abstract class AbstractClinicTests extends AbstractTransactionalDataSourceSpringContextTests {

protected Clinic clinic;

public void setClinic(Clinic clinic) {
this.clinic = clinic;
}

public void testGetVets() {
Collection vets = this.clinic.getVets();
assertEquals('JDBC query must show the same number of vets',
jdbcTemplate.queryForInt('SELECT COUNT(0) FROM VETS'),
vets.size());
Vet v1 = (Vet) EntityUtils.getById(vets, Vet.class, 2);
assertEquals('Leary', v1.getLastName());
assertEquals(1, v1.getNrOfSpecialties());
assertEquals('radiology', ((Specialty) v1.getSpecialties().get(0)).getName());
Vet v2 = (Vet) EntityUtils.getById(vets, Vet.class, 3);
assertEquals('Douglas', v2.getLastName());
assertEquals(2, v2.getNrOfSpecialties());
assertEquals('dentistry', ((Specialty) v2.getSpecialties().get(0)).getName());
assertEquals('surgery', ((Specialty) v2.getSpecialties().get(1)).getName());
}

JdbcTemplate 变量用于验证被测试的代码是否正确。JdbcTemplate 让测试更严密,且减少了对测试数据的依赖。例如,可以在不中止测试的情况下在数据库里增加额外的数据行。

PetClinic应用支持四种数据访问技术--JDBC、Hibernate、TopLink和JPA ,因此 AbstractClinicTests 类不指定Context位置,这个操作在子类中实现:

例如,用Hibernate实现的PetClinic测试包含如下方法:

public class HibernateClinicTests extends AbstractClinicTests {

protected String[] getConfigLocations() {
return new String[] {
'/org/springframework/samples/petclinic/hibernate/applicationContext-hibernate.xml'
};
}
}

8.3.6. 运行集成测试

集成测试比单元测试更依赖于测试环境,它是测试的一个补充,而不是代替单元测试的。这种依赖主要指完整数据模型的数据库。也可以通过DbUnit或者数据库工具来导入测试数据。

posted @ 2008-01-13 21:57 水仁圭 阅读(1437) | 评论 (0)编辑 收藏

Sentences01

(1) 最高级 + 名词 + that + 主词 + have ever seen /known /heard /had /read)
Helen is the most beautiful girl that I have ever seen.
海伦是我所看过最美丽的女孩。
Mr. Zhang is the kindest teacher that I have ever had.
张老师是我曾经遇到最仁慈的教师。

(2) Nothing is + 比较级 than to + V
Nothing is more important than to receive education.
没有比接受教育更重要的事。

(3) ... cannot emphasize the importance of ... too much.(再怎么强调...的重要性也不为过。)
We cannot emphasize the importance of protecting our eyes too much.
我们再怎么强调保护眼睛的重要性也不为过。

(4) There is no denying /doubt that从句 (不可否认的.../毫无疑问的...)
There is no denying that the qualities of our living have gone from bad to worse.
不可否认的,我们的生活品质已经每况愈下。
There is no doubt that our educational system leaves something to be desired.
毫无疑问的我们的教育制度令人不满意。

(5) It is universally acknowledged that从句 (全世界都知道...)
It is universally acknowledged that trees are indispensable to us.
全世界都知道树木对我们是不可或缺的。

(6)、An advantage of ... is that从句 (...的优点是...)
An advantage of using the solar energy is that it won't create (produce) any pollution.
使用太阳能的优点是它不会制造任何污染。

(7) The reason why定语从句 ... is that + 句子 (...的原因是...)
The reason why we have to grow trees is that they can provide us with fresh air.
The reason why we have to grow trees is that they can supply fresh air for us.
我们必须种树的原因是它们能供应我们新鲜的空气。

(8) So + 形容词 + be + 主词 + that从句 (如此...以致于...)
So precious is time that we can't afford to waste it.
时间是如此珍贵,我们经不起浪费它。

(9) Adj. + as + 主词 + be, S + V ... (虽然...)
Rich as our country is, the qualities of our living are by no means satisfactory.
{by no means = in no way = on no account 一点也不}
虽然我们的国家富有,我们的生活品质绝对令人不满意。

(10) 比较级 + S + V, 比较级 + S + V
The harder you work, the more progress you make.
你愈努力,你愈进步。
The more books we read,the more learned we become.
我们书读愈多,我们愈有学问。

(11) By +Ving, ... can ... (借着.. , ..能够..)
By taking exercise, we can always stay healthy.
借着做运动,我们能够始终保持健康。

(12) ... enable + 受词 + to + V (..使..能够..)
Listening to music enable us to feel relaxed.
听音乐使我们能够感觉轻松。

(13) On no account can we + V ... (我们绝对不能...)
On no account can we ignore the value of knowledge.
我们绝对不能忽略知识的价值。

(14) It is time + S + 过去式 (该是...的时候了)
It is time the authorities concerned took proper steps to solve the traffic problems.
该是有关当局采取适当的措施来解决交通问题的时候了。


(15) Those who ... (...的人...)
Those who violate traffic regulations should be punished.
违反交通规定的人应该受处罚。

(16) There is no one but ... (没有人不...)
There is no one but longs to go to college.
没有人不渴望上大学。{long v.渴望}

(17) be + forced /compelled /obliged + to do (不得不...)
Since the examination is around the corner, I am compelled to give up doing sports.
既然考试迫在眉睫,我不得不放弃做运动。

(18) It is conceivable /obvious /apparent that从句 (可想而知的 /明显的 /显然的)
It is conceivable that knowledge plays an important role in our life.
可想而知,知识在我们的一生中扮演一个重要的角色。

(19) That is the reason why ... (那就是...的原因)
Summer is sultry. That is the reason why I don't like it.
夏天很燠热。那就是我不喜欢它的原因。

(20) For the past + 时间,S + 现在完成式.. (过去...年来,...一直...)
For the past two years, I have been busy preparing for the examination.
过去两年来,我一直忙着准备考试。

(21) Since + S + 过去式,S + 现在完成式。
Since he went to senior high school, he has worked very hard.
自从他上高中,他一直很用功。

(22) It pays to + V ... (...是值得的。)
It pays to help others.
帮助别人是值得的。

(23) be based on (以...为基础)
The progress of thee society is based on harmony.
社会的进步是以和谐为基础的。

(24) Spare no effort to + V (不遗余力的)
We should spare no effort to beautify our environment.
我们应该不遗余力的美化我们的环境。

(25) bring home to + 人 + 事 (让...明白...事)
We should bring home to people the value of working hard.
我们应该让人们明白努力的价值。

(26) be closely related to ... (与...息息相关)
Taking exercise is closely related to health.
做运动与健康息息相关。

(27) Get into the habit of + Ving = make it a rule to + V (养成...的习惯)
We should get into the habit of keeping good hours.
我们应该养成早睡早起的习惯。

(28) [Due to | Owing to | Thanks to] + N/Ving, ... (因为...)
Thanks to his encouragement, I finally realized my dream.
因为他的鼓励,我终于实现我的梦想。

(29) [What a + adj. + sth. | How + adj. + sth.] + it is to do sth.(多么...!)
What an important thing it is to keep our promise!
How important a thing it is to keep our promise!
遵守诺言是多么重要的事!

(30) leave much to be desired (令人不满意)
The condition of our traffic leaves much to be desired.
我们的交通状况令人不满意。

(31) have a great influence on ... (对...有很大的影响)
Smoking has a great influence on our health.
抽烟对我们的健康有很大的影响。

(32) do good to (对...有益),do harm to (对...有害)
Reading does good to our mind.
读书对心灵有益。
Overwork does harm to health.
工作过度对健康有害。

(33) pose a great threat to ~~ (对...造成一大威胁)
Pollution poses a great threat to our existence.
污染对我们的生存造成一大威胁。

(34) do one's [utmost | best] to do sth.(尽全力去...)
We should do our utmost to achieve our goal in life.
我们应尽全力去达成我们的人生目标。

(35) would rather /sooner + V + than + V + (宁愿...也不)
They would rather go fishing than stay at home.
他们宁愿去钓鱼,也不愿待在家里。
She would sooner resign than take part in such dishonest business deals.
她宁可辞职也不愿参与这种不正当的买卖.



posted @ 2008-01-07 08:23 水仁圭 阅读(188) | 评论 (0)编辑 收藏

Struts 2 Tag用法

 

append 和 iterator

参考:http://www.roseindia.net/struts/struts2/struts2controltags/append-tag.shtml

 

在Action类的execute方法中,实例化List对象 

public String execute()throws Exception{
    myList = new ArrayList();
    myList.add("www.Roseindia.net");
    myList.add("Deepak Kumar");
    myList.add("Sushil Kumar");
    myList.add("Vinod Kumar");
    myList.add("Amit Kumar");

 

    myList1 = new ArrayList();
    myList1.add("www.javajazzup.com");
    myList1.add("Himanshu Raj");
    myList1.add("Mr. khan");
    myList1.add("John");
    myList1.add("Ravi Ranjan");
    return SUCCESS;
  }

 

jsp页面中使用append和iterator两个tag

 

<s:append id="myAppendList">
      <s:param value="%{myList}" />
      <s:param value="%{myList1}" />
</s:append>

   

<s:iterator value="%{#myAppendList}">
      <s:property /><br>
</s:iterator>


 

generator 和 iterator

参考:http://www.roseindia.net/struts/struts2/struts2controltags/generator-tag.shtml


 

在jsp中使用,'www.Roseindia.net,Deepak Kumar,Sushil Kumar,Vinod Kumar,Amit Kumar'这些内容被分行的显示在页面上。

<s:generator val="%{'www.Roseindia.net,Deepak Kumar,Sushil Kumar,Vinod Kumar,Amit Kumar'}" separator=",">
    <s:iterator>
      <s:property /><br/>
    </s:iterator>
</s:generator>

   
参考:http://www.roseindia.net/struts/struts2/struts2controltags/GeneratorTagCountAttribute.shtml

count="5" -->在jsp页面中显示前5个

<s:generator val="%{'www.Roseindia.net,Deepak Kumar,Sushil Kumar,Vinod Kumar,Amit Kumar, Sanjay, Vijay '}" count="5" separator=",">
   <s:iterator>
      <s:property /><br/>
   </s:iterator>
</s:generator>

参考:http://www.roseindia.net/struts/struts2/struts2controltags/GeneratorTagIdAttribute.shtml
<s:generator val="%{'www.Roseindia.net,Deepak Kumar,Sushil Kumar,Vinod Kumar,Amit Kumar'}" count="4" separator="," id="myAtt" />
<%
Iterator i = (Iterator) pageContext.getAttribute("myAtt");
while(i.hasNext()) {
  String s = (String) i.next(); %>
  <%=s%> <br/>
<% }
%>


 


iterator

参考:http://www.roseindia.net/struts/struts2/struts2controltags/iterator-tag.shtml

在Action类的execute方法中实例化一个List

public String execute()throws Exception{
    myList = new ArrayList();
    myList.add("Fruits");
    myList.add("Apple");
    myList.add("Mango");
    myList.add("Orange");
    myList.add("Pine Apple");
    return SUCCESS;
  }


 

在Jsp中可以通过list的名字来调用

<s:iterator value="myList">
    <s:property /><br>
</s:iterator>


 


merge

参考:http://www.roseindia.net/struts/struts2/struts2controltags/merge-tag.shtml
在Action类的execute方法中实例化两个List
public String execute() throws Exception{
    myList = new ArrayList();
    myList.add("www.Roseindia.net");
    myList.add("Deepak Kumar");
    myList.add("Sushil Kumar");
    myList.add("Vinod Kumar");
    myList.add("Amit Kumar");

    myList1 = new ArrayList();
    myList1.add("www.javajazzup.com");
    myList1.add("Himanshu Raj");
    myList1.add("Mr. khan");
    myList1.add("John");
    myList1.add("Ravi Ranjan");
    return SUCCESS;
  }


 

在jsp中,用merge tag把两个List合并,在iterator中用merge的id来调用

<s:merge id="mergeId">
        <s:param value="%{myList}" />
        <s:param value="%{myList1}" />

</s:merge>
<s:iterator value="%{#mergeId}">
    <s:property /><br>
</s:iterator>
显示顺序:
Display first element of the first list.
Display first element of the second list.
Display second element of the first list.
Display second element of the second list.
Display third element of the first list.
Display thrid element of the second list.....and so on.


subset

参考:http://www.roseindia.net/struts/struts2/struts2controltags/subsetTag.shtml

public String execute() throws Exception{
    myList = new ArrayList();
    myList.add(new Integer(50));
    myList.add(new Integer(20));
    myList.add(new Integer(100));
    myList.add(new Integer(85));
    myList.add(new Integer(500));
    return SUCCESS;
  }


 

调用Action类中的List

 <s:subset source="myList">
    <s:iterator>
      <s:property /><br>
    </s:iterator>
</s:subset>
在页面上显示前三个
<s:subset source="myList" count="3">
    <s:iterator>
      <s:property /><br>
    </s:iterator>
</s:subset>
在页面上显示从2开始的3个
<s:subset source="myList" count="3" start="2">
    <s:iterator>
      <s:property /><br>
    </s:iterator>
</s:subset>

action tag

参考:http://www.roseindia.net/struts/struts2/struts2controltags/action-tag.shtml

The action tag is a generic tag that is used to call actions directly from a JSP page by specifying the action name and an optional namespace. The body content of the tag is used to render the results from the Action. Any result processor defined for this action in struts.xml will be ignored, unless the executeResult parameter is specified.

在struts.xml中定义action映射
<action name="actionTag" class="net.roseindia.actionTag">
       <result name="success">/pages/genericTags/success.jsp</result>
</action>

public String execute() throws Exception{
    return SUCCESS;
  }


 

在jsp页面写入下面代码,那么当请求actionTag.action时,无论Action类net.roseindia.actionTag中怎么处理、如何设定页面转向,此请求直接转到successs.jsp页面

<s:action name="success">
    <b><i>The action tag will execute the result and include it in this page.</i></b></div>
</s:action>


 

bean tag


参考:http://www.roseindia.net/struts/struts2/struts2controltags/bean-tag.shtml
定义一个包含name属性的普通JavaBean,
public class companyName {
 
  private String name;

  public void setName(String name){
    this.name =name ;
  }

  public String getName(){
    return name;
  }
}


 

在jsp中调用

<s:bean name="net.roseindia.companyName" id="uid">
    <s:param name="name">RoseIndia</s:param>
    <s:property value="%{name}" /><br>
</s:bean>


date tag

参考:http://www.roseindia.net/struts/struts2/struts2controltags/date-tag.shtml

  private Date currentDate;
  public String execute() throws Exception{
    setCurrentDate(new Date());
    return SUCCESS;
  }

<s:date name="currentDate" format="MM/dd/yy" />

<s:date name="currentDate" format="MM/dd/yy hh:mm" />

<s:date name="currentDate" format="MM/dd/yy hh:mm:ss" />

Nice Date (Current Date & Time):<s:date name="currentDate" nice="false" />

Nice Date:<s:date name="currentDate" nice="true" />


 


include tage

是不是可以替换frame

<body>
    <h1><span style="background-color: #FFFFcc">Include Tag (Data Tags) Example!</span></h1>
      <s:include value="myBirthday.jsp" />
  </body>


param tag

参考:http://www.roseindia.net/struts/struts2/struts2controltags/param-tag.shtml

<ui:component>
        <ui:param name="empname">Vinod</ui:param><br>
        <ui:param name="empname">Amit</ui:param><br>
        <ui:param name="empname">Sushil</ui:param>
</ui:component>


Case 1. <param name="empname">Amit</param>  Here the value would be evaluated to the stack as a java.lang.String object.
Case 2. <param name="empname" value="Vinod"/> Here the value would be evaluated to the stack as a java.lang.Object object.


set tag

参考:http://www.roseindia.net/struts/struts2/struts2controltags/set-tag.shtml


 

set tag给指定范围内的变量赋值,得到name-value值对
赋值:<s:set name="technologyName" value="%{'Java'}"/>
调用:Technology Name: <s:property value="#technologyName"/>

set tag is used to assign a value to a variable in a specified scope. The parameters name and value in the tag <s:set name="technologyName" value="%{'Java'}"/> acts as the name-value pair. Here we set the parameters as name="technologyName" value="Java".

Text Tag

参考:http://www.roseindia.net/struts/struts2/struts2controltags/text-tag.shtml


 

struts.xml 文件中定义

<action name="textTag" class="net.roseindia.textTag">
       <result>/pages/genericTags/textTag.jsp</result>
</action>


在textTag.java文件所在包下,创建一个package.properties,内容如下:

webname1 = http://www.RoseIndia.net
webname2 = http://www.javajazzup.com
webname3 = http://www.newstrackindia.com


 

在jsp文件调用,如下,前三行显示package.properties对应信息;第四行显示Vinod, Amit, Sushil, .......;最后一行empname

<s:text name="webname1"></s:text><br>
<s:text name="webname2"></s:text><br>
<s:text name="webname3"></s:text><br>
<s:text name="empname">Vinod, Amit, Sushil, .......</s:text><br>
<s:text name="empname"></s:text>

property tag

参考:http://www.roseindia.net/struts/struts2/struts2controltags/property-tag.shtml


 

定义个JavaBean

public class companyName {
 
  private String name;

  public void setName(String name){
    this.name =name ;
  }

  public String getName(){
    return name;
  }
}


 

第二行给companyName的name属性赋值;第三行显示该值(RoseIndia),相当于调用了getName()方法;,

<s:bean name="net.roseindia.companyName" id="uid">
<s:param name="name">RoseIndia</s:param>
  <s:property value="%{name}" /><br>
</s:bean>
<!-- Default value -->
<s:property value="name" default="Default Value" />


<s:property value="%{name}" /> it prints the result of myBean's getMyBeanProperty() method.
<s:property value="name" default="Default Value" /> it prints the result of companyName's
getName() method and if it is null, print 'a default value' instead
.

 

 

posted @ 2007-12-30 19:43 水仁圭 阅读(3633) | 评论 (0)编辑 收藏

Struts 2 中Session的用法

在ActionSupport子类的execute方法中存储session
Map session = ActionContext.getContext().getSession();
session.put("logged-in","true");

 

同样在execute方法中,可以清除session变量
Map session = ActionContext.getContext().getSession();
session.remove("logged-in");

 

在jsp的head部分引入css文件
<head>
     <link href="<s:url value="/css/main.css"/>" rel="stylesheet" type="text/css"/> 
</head>

在jsp访问session
session Time: </b><%=new Date(session.getLastAccessedTime())%>

<a href="<%= request.getContextPath() %>/roseindia/logout.action">Logout</a>

 jsp中使用struts-tag访问session变量
<s:if test="#session.login != 'admin'">
 <jsp:forward page="/pages/uiTags/Login.jsp" /> 
</s:if>

posted @ 2007-12-30 18:02 水仁圭 阅读(5618) | 评论 (3)编辑 收藏