标签库:
a) struts框架下的struts标签库
b) sun jstl c标签库
作用: 1) jsp 和 java代码分离 -- 自定义标签
用标签来替代Java的代码
2) struts标签 能够和struts-config.xml
actionForm等特有的对象进行交互
struts标签库
html标签 --- 替代基本的html元素
bean标签 --- 访问JavaBean
logic标签 --- for
if判断 程序结构的标签化
html标签
1.html标签
<% taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
基本html标记的替代标签
a)基本的html元素标签 (选择使用)
<html:html> *****
<html:html locale="true"> 对应 <html lang="zh-CN">
<html:base> *****
<html:base/> 对应 <base href="http://localhost:8080/aa.jsp">
当前页面的地址
<html:img> *******
<html:img page="images/zhangying.gif"/>
对应
<img src="images/zhangying.gif">
传参数
<html:img page="jspimage.jsp" paramId="id" paramName="1"/>
对应
<img src="jspimage.jsp?id=1"/>
**** <html:link> 3个属性page href forward
代替Html中超链接标记
<a href="sueccess.jsp">跳转</a>
<html:link page="logout.jsp"> 相对路径page
注销
</html:link>
对应
<a href="logout.jsp">注销</a>
<html:link href="http://www.sohu.com"> 绝对路径href
sohu网站
</html:link>
对应
<a href="http://www.sohu.com">sohu网站</a>
struts-config.xml中的global-forwards
forward
<html:link forward="success"> 对应全局转发
成功页面
</html:link>
对应
<a href="/success.jsp">成功页面</a>
传参数:
paramId 指定传参数的名字 id
paramName 指定参数的值 1
<html:link page="/deleteemp.do" paramId="id" paramName="1">
删除
</html:link>
对应
<a href="/deleteemp.do?id=1">删除</a>
<html:rewrite> 输出对应的URI 的字符串
不产生超链接
<html:rewrite page="/deleteemp.do" paramId="id" paramName="1">
对应
/deleteemp.do?id=1
b)基本表单标签 ->form表单 ******
<html:form>
<html:text>
<html:hidden>
<html:reset>
<html:submit>
<html:password>
<html:checkbox>
<html:radio>
<html:file> ----- 文件类型
<html:select>
<html:option>
对应基本的表单元素 property="" value=""
<input type="text" name="user" value="">
对应于
<html:text property="user" value=""/>
<input type="checkbox" name="ah" value="1">体育
<input type="checkbox" name="ah" value="2">游泳
对应于
<html:checkbox property="ah" value="1">体育</html:checkbox>
<html:checkbox property="ah" value="2">游泳</html:checkbox>
<html:file> ---文件的上传的标签
common-fileupload-1.0.jar
实现文件的上传
actionForm中 FormFile的类型 代表file文件
用处: <html:file/> 文件上传得
<html:link/> 页面跳转的
c)错误和正确信息标签 *******
jdk的bin中 native2ascii.exe
编码方式变为Unicode方式
native2ascii -encoding gb2312 applicationResources.properties
ApplicationResources_zh_CN.properties
<html:errors/>
输出所有actionError的内容
<html:errors property="uname"/>
输出key=uname对应的内容
2.bean标签 访问变量的属性或输出值
访问JavaBean
a)访问http请求信息和jsp隐式对象
** <bean:cookie> cookie访问
<bean:cookie id="mycookie" name="loginname" value="123"/>
id -- 自己设置Cookie变量的名字
name -- Cookie对象的名字
value -- 默认值
<bean:write name="mycookie" property="value"/>
取出值
<bean:write name="mycookie" property="name"/>
取出名字
Cookie c = new Cookie("loginname","northwind");
c.getValue()
c.getName()
*** <bean:parameter>访问请求的参数request
<bean:parameter id="arg1" name="username" value="north"/>
<bean:write name="arg1"/>
对应的Java代码
request.getParameter("username");
**** <bean:page> 访问jsp的隐式对象---属性
session
getId() -> id
getCreationTime() -> creationTime
application
request
response
所有对象的get方法能访问到的属性 都可以被这个标签访问
属性的第一个字母变成小写后都可以访问
<bean:page id="this_session" property="session"/>
<bean:write name="this_session" property="creationTime"/>
b)访问资源
*** <bean:message> 访问applicationResources中的消息
<bean:message key="hello.user"/> 输出key的值
<bean:include> 包含资源到一个变量中
动作 <jsp:include page="testpage1.jsp"/>
页面运行结果包含进来
等价于下面2句话
<bean:include id="tp1" page="/testpage1.jsp"/>
<bean:write name="tp1"/> 输出变量的内容
输出的是页面的内容
c) 定义和输出JavaBean
***** <bean:define> 定义一个变量
<bean:define id="aa" value="Hello"/>
<bean:write name="aa"/> -> Hello
<bean:define id="bb" name="empbean" property="ename"/>
JavaBean:empbean
属性ename
变量 String bb = empbean.getEname();
<bean:write name="bb"/> 得到empbean中属性ename的值
*** <bean:write> 显示JavaBean及其属性或变量的内容
<bean:write name="" property="" scope=""/>
name=某个范围中的属性的名字 (对象)
property=JavaBean中的属性
scope=访问对象的范围
3.logic标签
a)if else
if a=b then 等于
不等于
大于
小于
字符串
匹配
不匹配
空值
非空值
b)循环
for(int i=0;i<vect.size();i++){
}
对集合的遍历如何做??
a)
存在性
<logic:present/> 是否存在某种变量
cookie="cookie名字"
header="request中的header"
parameter="参数"
name="变量或JavaBean" property=""
scope="request/session/application"
如果存在 就输出标签体的内容
相反的标签
<logic:notpresent/>
是否为空
空 - ""或null
<logic:empty name="变量或JavaBean" property="">
</logic:empty>
是否相等
<logic:equal/>
大于 > <logic:greaterThan/>
>= <logic:greaterEqual/>
小于 < <logic:lessThan/>
<= <logic:lessEqual/>
匹配
<logic:match/>
<logic:nomatch/>
location 表示的是位置
从开始匹配 start
从结束匹配 end
循环
<logic:iterate>
<%
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
request.setAttribute("number",v);
%>
id定义一个变量
name定义变量的内容
<logic:iterate id="ele" name="number">
<bean:write name="ele"/>
</logic:iterate>
对集合中所有元素的循环访问
indexId 定义循环计数器变量 i
offset 开始元素的位置
length 要输出元素的个数
<logic:iterate id="ele" indexId="index" name="number" offset="1" length="2">
<bean:write name="index"/>.<bean:write name="ele"/><br>
</logic:iterate>
c) logic转发和重定向
<logic:forward name="index"/> 对应global-forwards
转发到index
<jsp:forward page="index"/>
<logic:redirect page="aa.jsp"/>
重定向到aa.jsp
response.sendRedirect("aa.jsp")
struts中文乱码问题的解决
<1>编码转换函数
<2>过滤器
Action和jsp的开发其实就是对Struts标签的运用.掌握标签的熟练程度决定了开发效率.初学者往往对某个数据表示或数据获取,束手无策.一个简单的问题浪费一两天时间也就不足为怪了.导致整个开发进度延后.外面的struts书籍介绍标签和数据传输原理都比较简单,下面我对标签技术和数据传输原理,进行全方位多角度的剖析.希望对各位有所帮助.以此为模版,将大大提高开发效率.以sample为机能名称.
①画面上有一text框,显现内容为某一数据表中的某一字段.那我们该如何设置和得到此数据呢?
SampleJsp:
< html:text name = "sampleForm" property="name" />
SampleForm.java: // form文件名必须和jsp中标签的name对应
String name; // 必须和jsp中该项目的property一样
public String getName() { return name; }
public void setName(String name) { this.name = name;}
变量和方法名,不可以顺意.变量abcd,那方法名就是setAbcd和getAbcd.注意大小写.
jsp中的项目必然全部在form里面有所表示,当然反过来,form里的项目在jsp中不一定全部表示(可能有辅助动作的对象或验证)
SampleAction.java
public ActionForward start(ActionMapping mapping,
ActionForm argForm, HttpServletRequest req, HttpServletResponse res)
throws Exception {
SampleForm form = (SampleForm) argForm;
String name = ………………other codes for get name from db
// set name
form.setName(name);
// now text will show the name
}
public ActionForward save(ActionMapping mapping,
ActionForm argForm, HttpServletRequest req, HttpServletResponse res)
throws Exception {
SampleForm form = (SampleForm) argForm;
// get name
String name = form.getName();
………………other codes for save name
}
jsp和form对应,action操作form,form其实起了传输数据的作用.这就是struts标签的核心原理.得到数据和设置数据没问题了,剩下的工作也就得心应手了.
②再看一个处理标签的方法.画面上是一个明细一览表示(表).表示的是数据表user的相关数据(id,name).
SampleJsp:
< logic:present name="sampleForm" property="userList" >
< logic:iterate id="user" name=" sampleForm " property="userList">
< tr>
< td>< bean:write name="user" property="id" />< /td>
< td>< bean:write name="user" property="name" />< /td>
< /tr>
< /logic:iterate>
< /logic:present>
logic:present是逻辑判断,sampleForm中userList为空(无数据或null),下面的东东不显示.
logic:iterate是逻辑循环,userList有几条数据,就循环几次.
< bean:write name="user" property="id" />是lable标签,显示user这个对象(entity)的id属性.或者说显示数据表user中的一条记录中的id这个列.
User.java(就是entity,因为和业务密切,高达不开发,切记切记不可顺意修改.遇到设计有问题,QA日本)
String id;
public String getId() { return id; }
public void setId(String id) { this.id = id; }
String name;
public String getName () { return name; }
public void setName (String name) { this.name = name; }
看到这,是否觉得面熟啊,好象和FORM一样,但有点不一样,不一样在哪里,看下去后,自己感悟吧.
SampleForm.java:
List userList;
public List getUserList () { return userList; }
public void setUserList (List userList) { this.userList = userList; }
form只要这些,那你会问,id和name,struts如何能得到呢?你不是说过jsp必须和form一样对应吗?不错,一一对应是肯定的. UserList信息已经包含了一切,还需要定义id和name吗?至于struts如何得到数据,那就看下面的action是如何处理的吧.
SampleAction.java
public ActionForward start(ActionMapping mapping,
ActionForm argForm, HttpServletRequest req, HttpServletResponse res)
throws Exception {
SampleForm form = (SampleForm) argForm;
ArrayList userList = new ArrayList();
User user = new User();
user.setId(1);
user.setName(“name1”);
userList.add(user);
User user = new User();
user.setId(2);
user.setName(“name2”);
userList.add(user);
// set userList
form.setUserList(userList);
// now table will show
}
一切搞定.是不是很简单,但估计你还是有点晕.你还是想问我,id和name到底是如何设置的?
Action设置了userList就够了,它包含够多的信息了. struts看见了你设置了userList.它就知道了这个list里面都user(entity),useruser(entity)里面不是有很多get,set方法吗?
再看下下面的东东.
< logic:iterate id="user" name=" sampleForm " property="userList">
< bean:write name="user" property="id" />
id=”user”,和name="user" 对应了,明白啥意思吗?.就象循环指明索引一样. property="id"就是要显示的这个索引对应的内容.Struts就是这样来认id和name的.
③接下来,看一个加强版的table例子,在显示的明细一览,每一行前面加一个radio框,让用户选择哪个user.进行删除操作.
SampleJsp:
< logic:present name="sampleForm" property="userList" >
< logic:iterate id="user" name=" sampleForm " property="userList">
< tr>
< td>
< html:radio name="sampleForm" property="selectedUserId" value="< %=((jp.co.mhcb.obs.persis.entity.User)pageContext.getAttribute("user ")).getId().toString() %>" />
< /td>
< td>< bean:write name="user" property="id" />< /td>
< td>< bean:write name="user" property="name" />< /td>
< /tr>
< /logic:iterate>
< /logic:present>
sampleForm.java:
String selectedUserId;
public String getSelectedUserId () { return selectedUserId; }
public void setSelectedUserId(String selectedUserId) {
this.selectedUserId = selectedUserId;
}
SampleAction.java
public ActionForward delete(ActionMapping mapping,
ActionForm argForm, HttpServletRequest req, HttpServletResponse res)
throws Exception {
SampleForm form = (SampleForm) argForm;
String selectedUserId = form.getSelectedUserId();
// get user by selected id
User user = getUser(selectedUserId);
// delete user
}
radio框. propertys值对应form里的对象.value值是该行radio对应的user中的id(数据表中user的id是主键),那么当用户选中任何一个radio,struts通过form得到propertys值,就可以得到选中哪个user了,然后进行相应操作.
设置哪个user被选中,一是通过用户选择,没的说.二,通过程序控制,如果进入初期画面,我要让user.id = ‘3’的radio被选中,只要在初期Action中form.selectedUserId(“3”);一切搞定,就一句话,进入初期画面时, user.id = ‘3’的radio被选中了.
注意以下标签
< html:radio name="sampleForm" property="selectedUserId" value="< %= ((jp.co.mhcb.obs.persis.entity.User)pageContext.getAttribute("user ")).getId().toString() %>" />
下面发挥想象一下以下标签啥意思?
< html:radio name="sampleForm" property="selectedUserId" value="< %= ((jp.co.mhcb.obs.persis.entity.User)pageContext.getAttribute("user ")).getObject1().getObject1().getObject2()…………getObjectN().getId().toString() %>" />
能看出来什么?
User包含object1,object2包含object3,….objectN-1包含objectN,objectN有id属性.
看出来了吗?灵活运用,想象一下,各个entity和form,action该如何写?
④接着介绍一下,checkbox是使用.画面有一排checkbox,如何设置和得到数据呢?先看一个简单点的.
< html:checkbox name=" sampleForm" property="chechbox1" value="true" />
< html:checkbox name=" sampleForm" property="chechbox2" value="false" />
< html:checkbox name=" sampleForm" property="chechbox3" value="true" />
第二个框未选中,其他选中.form里面对应三个String chechbox1,chechbox2, chechbox3;下面来个复杂点的,多选择对话框multibox
SampleJsp中:
< logic:iterate name = "sampleForm" id="user" property="userList">
< html:multibox property="selectedUsers">
< bean:write name="user" property="id"/>
< /html:multibox>
< bean:write name="user" property="name"/>
< /logic:iterate>
SampleForm中:
private String userList[] = new String[0];
public String[] getUserList () { return userList;}
public void setUserList(String[]userList) {this.userList = userList;}
private String selectedUsers[] = new String[0];
public String[] getSelectedUsers () {return selectedUsers;}
public void setSelectedUsers (String[]selectedUsers) {this.selectedUsers = selectedUsers;}
如果我们在初期时在action里对bean赋值:
userList = { User(”1”,”name1”), User(”2”, ”name2”), User(”3”,”name3”) }
selectedUsers = {“1”,”3”}
那画面选中第一第三个选择框.
用户修改选择框,选择了第二,第三个,那么在action里取bean的值
String selectedItems[] = new String[list.getSize()];
selectedItems = form.getSelectedItems();
for ( int i = 0 ; i < selectedItems.length ; ++i ){
LOGGER.debug( "selected " + i + ": " + selectedItems[i]);
}
Selected 0 : 2
Selected 1 : 3
selectedUsers = {“2”,”3”}
⑤画面上有一user表,每条数据前面有个button,对应一条记录,如何确定选中那条数据呢??
SampleJsp:
< logic:iterate id="user" indexId="buttonIndex" name="sampleForm" property="userList">
< tr>
< td>
< html:submit property="button" indexed='false' >
< bean:message key="label.button.selectUser"/>
< /td>
< td>< bean:write name="user" property="id" />< /td>
< td>< bean:write name="user" property="name" />< /td>
< /tr>
< html:hidden name="sampleForm" property="selectUserIndex" value='< %= "" + buttonIndex %>'/>
< /logic:iterate>
SampleAction.java
int index = Integer.parseInt(form.getSelectUserIndex());
通过一个隐藏变量,得到选中第几条数据,然后就能做相应处理.
⑥上面都是通过form和jsp传输数据的.还有session也能让jsp显示数据.但如果我做为设计者,是不提倡这样做的.为什么就不说了.但日本以前的设计很可能会用到session和jsp传数据.那我就有必要讲一下如何用了?做为高达的设计者还是尽量不要用session和jsp沟通.
有个下拉列表框,里面显示所有用户名称.用session传数据.
SampleJsp:
< %pageContext.setAttribute("userList",(List) (FwThreadContext
.getAttribute("AllUser")));
%>
< html:select property="selectedUser">
< html:options collection="userList" property="id" labelProperty="name" />
< /html:select>
SampleForm.java:
String selectedUser;
Form里只要一个selectedUser,表示选择的user. 下拉列表框用session表示.
在action等地方设置了session的内容,那下拉列表框就能显示内容了.这里session名为AllUser, labelProperty="name"是下拉列表框显示的东东, property="id"是下拉列表框每条数据隐藏的东东.通过property="selectedUser"里得到选中那条数据
< html:text name="sampleForm" property="name"
value="< %= (FwThreadContext.getAttribute("UserName")).toString() %>" />
这里很简单就是把session名为UserName设置到Text框中.得的时候还是通过form中的name得到.
标签宝典:
1,lable
< bean:write name="sampleForm" property="name" />
2,text
< html:text name="sampleForm " property="name" />
3,button
< html:submit property="button">
< bean:message key="label.button.save" />
< /html:submit>
< html:button property="button" οnclick="javascript:openCalendar(date);">
< bean:message key="label.button.date" />
< /html:button>
4,select
< html:select property="selectedUser">
< html:options name="sampleForm" collection="userList" property="id" labelProperty="name" />
< /html:select>
5,checkbox,multibox
< html:checkbox name="sampleForm" property="chechbox1" value="true" />
< logic:iterate name = "sampleForm" id="user" property="userList">
< html:multibox property="selectedUsers">
< bean:write name="user" property="id"/>
< /html:multibox>
< bean:write name="user" property="name"/>
< /logic:iterate>
6, 循环逻辑
< logic:present name="sampleForm" property="userList" >
< logic:iterate id="user" name=" sampleForm " property="userList">
< tr>
< td>
< html:radio name="sampleForm" property="selectedUserId" value="< %= ((jp.co.mhcb.obs.persis.entity.User)pageContext.getAttribute("user ")).getId().toString() %>" />
< /td>
< td>< bean:write name="user" property="id" />< /td>
< td>< bean:write name="user" property="name" />< /td>
< /tr>
< /logic:iterate>
< /logic:present>
7,if逻辑
< logic:equal name=" sampleForm " property="showAllFlg" value="true" >
< html:submit property="button">
< bean:message key="label.button.all"/>
< /html:submit>
< /logic:equal>
< logic:equal name=" sampleForm " property=" showAllFlg " value="false" >
< html:submit property="button">
< bean:message key="label.button.noall"/>
< /html:submit>
< /logic:equal>