在JAVAEE规范中,存在三种标签
(1)自定义标签
(2)jstl标签
(3)struts标签
jstl标签实际上可以完成标签任务
当 struts设计者,在提供struts框架后,同时提供一套标签库,命名为struts标签库
在实际开发中,jstl可以和struts标签混合使用
mvc结构->jstl
struts框架->推荐使用struts标签来完成jsp页面
但是实际上 两种标签可以相互补充
get比post还有一个好处:更加利于用于收藏该页面
Struts标签的介绍
html标签(常用,重点)
bean标签(常用,重点)
logic标签(常用,重点)
struts版本用户管理系统
struts标签
html标签
该类标签实际上就是普通的html标签,struts设计者主要为了和struts框架统一,对普通的html标签做了一个封装,本质仍然后html标签
说明:*.tld叫标签描述文件(tag library discription)
html:cance/
例子
1.创建一个项目,引入struts框架
2.查找标签库的链接
3.引入标签库
<!-- 引入struts标签库的html标签 -->
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
举例html.jsp
<body>
<!-- property:就是buttun的name -->
<html:button property="mybuttun" value="提交"></html:button>
<%--image实际上是一个图片按钮 img就是普通图片 --%>
<html:image src="images/logo.jpg"></html:image>
<html:img src="images/logo.jpg"/>
<html:link href="http://www.sohu.com">连接百度</html:link>
<%--html:form
struts的html标签如果涉及到输入信息,struts框架认为
1.要求和一个表单类对应,应当放到一个<html:form>中
--%>
<html:form action="/login.do" method="post">
u:<html:text property="name"></html:text>
<html:checkbox property="fruit" value="applece">apple</html:checkbox>
</html:form>
</body>
说明:在使用struts的html标签时,也可以同时使用普通html标签
createUser.jsp //jsp充当view,作界面
UaweForm.java //ActionForm,用户接受用户输入
UserAction.java //Action作控制器,获得ActionForm数据并处理
User.java //bean作模型,在action中被调用,完成任务
UserService.java //为了更好的管理数据,而写的一个类
SqlHelper.java //操作数据库工具类
使用过滤器实现关键字的拦截
关键字可能经常变化,怎样做更加灵活?
思路:可否在filter中配置关键字,然后读入
bean标签库
Bean标签库为访问Bean和它的属性提供了方便的机制
1.该标签库包含的标签可以用来创建bean,访问bean和访问bean的属性
2.提供了依据cookies,headers和parameters的值创建相关bean的能力
bean标签的使用
引入标签库
<!-- 导入bean标签库 -->
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
1.bean:write
<%
//模拟代码:这些代码将来在控制器写
request.setAttribute("abc", "hello.world");
//创建Cat
Cat cat=new Cat("xiaoming",30);
request.setAttribute("cat1", cat);
%>
<!-- bean:write 用于输出信息 -->
<bean:write name="abc"/><br>
<!-- 输出对象的属性值 -->
<bean:write name="cat1" property="name"/>
<bean:write name="cat1" property="age"/>
2.<bean.message>
该标签通常和资源文件配置使用,可以更加方便地输出信息.在资源文件
ApplicationResource.properties
# Resources for parameter 'com.hyf.struts.ApplicationResources'
# Project strutstag
key1=welcome {0}
# 在实际开发中,我们可以把错误号统一,在需要显示的时候,直接使用
# oracle中,ora001 ora002
err1 =\u4f60\u7684\u9519\u8bef\u662f {0} and {1}
这样使用:
<h1>bean:massage</h1>
<bean:message key="key1" arg0="jone"/><br>
<bean:message key="err1" arg0="用户名为空" arg1="密码也为空"/><br>
解决中文乱码问题:
使用jsp:userBean也可以创建bean/domain对象
方法
<h1>jsp-userbean的用法</h1>
<jsp:useBean id="cat2" class="com.hyf.domain.Cat"></jsp:useBean>
<jsp:setProperty property="name" name="cat2" value="tom"/>
<jsp:setProperty property="age" name="cat2" value="12"/>
<%
//上面3句<=>Cat cat2=new Cat("tom",12);
%>
${cat2.name }
Login标签的使用
引入logic标签库
<!-- 引入logic标签库 -->
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>
login主要用于管理和条件相关的输出,对某些对象进行迭代输出,和流程管理。
<body>
<h1>logic标签的使用 相当于jstl c:foreach</h1>
<%
Cat cat1=new Cat("aa",12);
Cat cat2=new Cat("bb",24);
Cat cat3=new Cat("cc",27);
ArrayList<Cat> list=new ArrayList<Cat>();
list.add(cat1);
list.add(cat2);
list.add(cat3);
request.setAttribute("cats", list);
%>
<logic:iterate id="mycat" name="cats">
${mycat.name}<%--<bean:write name="mycat" property="name"/>--%>||
${mycat.age}<%--<bean:write name="mycat" property="age"/><br>--%>
</logic:iterate>
<logic:empty name="ee">
ee属性不存在
</logic:empty>
<logic:notEmpty name="cats">
cats属性存在
</logic:notEmpty>
<h1>logic标签可以判断大小</h1>
<logic:iterate id="mycat" name="cats">
<logic:greaterThan name="mycat" value="20" property="age">
<bean:write name="mycat" property="name"/>年龄大于20<br>
</logic:greaterThan>
</logic:iterate>
</body>
这里给出一个选择标签的原则
Jstl->el->struts