提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
上一篇,我们学习了struts的基本概念,怎样搭建struts开发环境,从编写formbean,action到jsp页面,以及怎样将他们配入到struts-config.xml文件中,实现通过浏览器发送请求,将处理结果返回页面简单的login功能。
今天我们来学习下struts为我们提供的常用标签,标签是视图技术,主要用在jsp中,常用的功能是,在action中设置到request域或session域中的数据,进行空判断,日期数字格式化,基本数据类型,字符串或javabean中的对象以及html文本直接或转义,最终输出到页面,还可以遍历集合。
struts标签和国际化绑定,在struts-config.xml文件加入下记内容
<struts-config>
中间略
<message-resources parameter="MessageResources" /> ←追加的内容
中间略
</struts-config>
在jsp页面中第二行引入下记指令
struts有很多标签库,我们今天主要来学习常用的bean和logic标签
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
action代码如下(示例):
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import xxx.xxx.pojo.Group;
import xxx.xxx.pojo.User;
public class BeanWriteAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
request.setAttribute("hello", "helloworld");
request.setAttribute("htmlText", "<font color='red'>text red color</font>");
request.setAttribute("today", new Date());
request.setAttribute("n", 12345.1234);
Group group = new Group();
group.setName("group1");
User user = new User();
user.setUsername("zhang san");
user.setAge(28);
user.setGroup(group);
request.setAttribute("user", user);
return mapping.findForward("success");
}
}
代码如下(示例):
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>测试beanwrite标签</h1>
<hr>
<li>普通字符串</li>
hello(标签):<bean:write name="hello"/><br>
<p></p>
html 文本(defalut):<bean:write name="htmlText"/><br>
html 文本(filter=false):<bean:write filter="false" name="htmlText"/><br>
<li>格式化日期</li>
<p></p>
系统时间(default):<bean:write name="today"/><br>
系统时间(format):<bean:write name="today" format="yyyy-MM-dd HH:mm:ss"/><br>
<li>格式化数字</li>
<p></p>
数值(default):<bean:write name="n"/><br>
数值(format):<bean:write name="n" format="###,###.00000#"/><br>
<li>javabean</li>
<p></p>
姓名:<input type="text" name="username" value="<bean:write name="user" property="username"/>"><br>
年龄:<input type="text" name="age" value="<bean:write name="user" property="age"/>"><br>
分组:<input type="text" name="groupname" value="<bean:write name="user" property="group.name"/>"><br>
</body>
</html>
< bean:write>运行效果
action代码如下(示例):
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import xxx.xxx.pojo.Group;
import xxx.xxx.pojo.User;
public class LogicAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
request.setAttribute("attr1", null);
request.setAttribute("attr2", "");
request.setAttribute("attr3", new ArrayList());
return mapping.findForward("success");
}
}
jsp代码如下(示例):
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>测试logic标签(empty,notempty,present,notpresent)</h1>
<hr>
<logic:empty name="attr1">
attr1为空<br>
</logic:empty>
<logic:notEmpty name="attr1">
attr1不为空<br>
</logic:notEmpty>
<logic:present name="attr1">
attr1存在<br>
</logic:present>
<logic:notPresent name="attr1">
attr1不存在<br>
</logic:notPresent>
<p>
<logic:empty name="attr2">
attr2为空<br>
</logic:empty>
<logic:notEmpty name="attr2">
attr2不为空<br>
</logic:notEmpty>
<logic:present name="attr2">
attr2存在<br>
</logic:present>
<logic:notPresent name="attr2">
attr2不存在<br>
</logic:notPresent>
<p>
<logic:empty name="attr3">
attr3为空<br>
</logic:empty>
<logic:notEmpty name="attr3">
attr3不为空<br>
</logic:notEmpty>
<logic:present name="attr3">
attr3存在<br>
</logic:present>
<logic:notPresent name="attr3">
attr3不存在<br>
</logic:notPresent>
<p>
</body>
</html>
< logic:empty>,< logic:notempty>,< logic:present >,< logic:notPresent>运行效果
action代码如下(示例):
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import xxx.xxx.pojo.Group;
import xxx.xxx.pojo.User;
public class LogicIterateAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
Group group = new Group();
group.setName("group1");
List<User> users = new ArrayList<>();
for (int i = 0; i< 10; i++) {
User user = new User();
user.setUsername("zhang san_"+i);
user.setAge(28+i);
user.setGroup(group);
users.add(user);
}
request.setAttribute("users", users);
return mapping.findForward("success");
}
}
jsp代码如下(示例):
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>测试logic迭代标签</h1>
<hr>
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<logic:empty name="users">
<tr>
<td colspan="3">没有符合条件的数据</td>
</tr>
</logic:empty>
<logic:notEmpty name="users">
<logic:iterate id="user" name="users">
<tr>
<td>
<bean:write name="user" property="username"/>
</td>
<td>
<bean:write name="user" property="age"/>
</td>
<td>
<bean:write name="user" property="group.name"/>
</td>
</tr>
</logic:iterate>
</logic:notEmpty>
</table>
</body>
</html>
< logic:iterate>运行效果
以上就是struts为我们提供的基本标签的使用,以后我也会随时更新项目中一些常用的标签,下篇见。