您的位置:首页 > 编程语言 > Java开发

Java开发者需要坚持的10大准则

2010-03-25 11:45 274 查看
一、date_select(object_name,method,options={},html_options={})
1、use_month_numbers
用法: :use_month_numbers=>true
月份默认以英文形式显示,当该属性设置为true后,则以数字的形式显示;
2、start_year
用法: :start_year=>1990
默认的起始年为当年减5年;若要自己设定,可以用start_year进行设定;
3、end_year
与start_year类似
4、discard_day
:discard_day=>true
去除年、月、日选项中的“日”,并默认为所选择的“月”的第一天
5、discard_month
:discard_month=>true
去除年、月、日选项中的“月”,在设置discard_month设置为true时,discard_day也会自动设置为true
6、discard_year
去除年、月、日选项中的“年”
7、default
:default=>{:day=>20}
当日期没有被选择,或被选择的日期格式无效时,所设定的默认值

二、表单form
以前的用法为:
<%form_tag :action=>'',:method=>'post' %>
<%=label_tag 'Name'%>
<%=text_field :table_name,:attr1%>
<%=check_box :table_name,:attr2%>
<%=submit_tag 'create'%>
<%end%>

新方法:

<%form_for :table_name,:url=>{:action=>''},:html=>{:id=>'test_id;} do |f|%>
<%=f.label :attr1%>
<%=f.text_field :table_name,:attr1%>
<%=f.check_box :attr2%>
<%=submit_tag 'create'%>
<%end%>

三、collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
有时候可以很好的代替select标签
例:
<%=collection_select(:child,:parent_id,@parent,:parent_id,:parent_name%>

四、field_set_tag
将其内部的内容包含在<fieldset><lenged></lended></fieldset>之中,可以美化界面
<% field_set_tag do %>
<p><%= text_field_tag 'name' %></p>
<% end %>

五、数据处理
1,number_to_currency(number,options={})
options:
:precision-设置货币数字的精度(默认为小数点后两位)
:unit - 设置货币符(默认为$)
:separator-设置小数点的分隔符(默认为".")
:delimiter-设置千位的分隔符(默认为",")

2,number_to_human_size(number,options)
:precision,:separator,:delimiter;
将数字转换为字节
number_to_human_size(123) # => 123 Bytes
number_to_human_size(1234) # => 1.2 KB

3,number_to_percentage(number,options)
4,number_with_delimiter()
为数字添加分隔符
number_with_delimiter(12345678, :delimiter => ".") # => 12.345.678
number_with_delimiter(12345678, :seperator => ",") # => 12,345,678
5,number_with_precision(number,options)
位数字设定精度
umber_with_precision(111.2345, :precision => 2) # => 111.23
number_with_precision(13, :precision => 5) # => 13.00000

六、文字安全
1、sanitize,sanitize_css
去除页面文字中包含javascript,href等不安全字符

2,strip_links
除去链接中不安全的字符如:
str='<a href="http://www.baidu.com">baidu</a>'
strip_links(str) =>baidu

3、strip_tags
例:
str='<div style="display:none">some words</div>'
strip_tags(str) =>some words

七、文字美化
1、auto_link
功能:为包含<a></a>的文字自动添加链接,为包含@的文件自动添加邮件链接
例如:
<%=auto_link("<a href='http://www.baidu.com'>baidu</a> peswe@163.com")%><br>

2、concat
功能:类似ASP中的response.write
用法:concat 'content',binding

3、cycle
功能:在表格的行或其他的元素中循环显示不同的css属性
例如:
<style type="text/css">
.even{
background-color:olive;
}
.odd{
background-color:fuchsia;
}
</style>
<table border="0">
<%(0..10).each do |i|%>
<tr class="<%=cycle('even','odd')%>">
<td>
some words
</td>
</tr>
<%end%>
</table>

4、excerpt
功能:在搜索时常用,突出显示某些关键词
例如:
excerpt('This is an example', 'an', :radius => 5)
# => ...s is an exam...

excerpt('This is an example', 'is', :radius => 5)
# => This is a...

5、highlight
功能:突出显示某些关键词
highlight('You searched for: rails', 'rails')
# => You searched for: <strong class="highlight">rails</strong>

highlight('You searched for: ruby, rails, dhh', 'actionpack')
# => You searched for: ruby, rails, dhh

highlight('You searched for: rails', ['for', 'rails'], '<em>\1</em>')
# => You searched <em>for</em>: <em>rails</em>
在最后的那个例子中,<em>\1</em>中的1是数字1,而不是字母L

6、pluralize
功能:更具情况将对象转为复数
pluralize(1, 'person')
# => 1 person

pluralize(2, 'person')
# => 2 people

pluralize(3, 'person', 'users')
# => 3 users

pluralize(0, 'person')
# => 0 people
7、simple_format
功能:为段落中的字符添加<p>标签,且在段落中出现"\n"的地方自动加入"<br>"
例:
my_text = "Here is some basic text...\n...with a line break."

simple_format(my_text)
# => "<p>Here is some basic text...\n<br />...with a line break.</p>"

more_text = "We want to put a paragraph...\n\n...right there."

simple_format(more_text)
# => "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>"

simple_format("Look ma! A class!", :class => 'description')
# => "<p class='description'>Look ma! A class!</p>"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: