struts标签的配置和使用

struts标签的配置和使用

配置:
* 在struts-config.xml文件中加入
<message-resources parameter="MessageResources" />
* 拷贝MessageResources.properties文件到src下
使用:
* 采用taglib指令引入
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>


MessageResources.properties

[quote]# -- standard errors --
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title=Struts Blank Application
welcome.heading=Welcome!
welcome.message=To get started on your own application, copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's "webapp" folder (or equivalent), and let your container auto-deploy the application. Edit the skeleton configuration files as needed, restart your container, and you are on your way! (You can find the application.properties file with this message in the /WEB-INF/src/java/resources folder.)[/quote]


BeanWriteTestAction.java


package com.bjsxt.struts;

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;

/**
* 测试BeanWrite
* @author Administrator
*
*/
public class BeanWriteTestAction extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//普通属性
request.setAttribute("hello", "Hello World");

//html文本
request.setAttribute("bj", "<font color='red'>北京欢迎您</font>");

//日期
request.setAttribute("today", new Date());

//数字
request.setAttribute("n", 123456.987);

//结构
Group group = new Group();
group.setName("尚学堂");

User user = new User();
user.setUsername("张三");
user.setAge(18);
user.setGroup(group);

request.setAttribute("user", user);

return mapping.findForward("success");
}

}



User.java


package com.bjsxt.struts;

public class User {

private String username1;

private int age;

private Group group;

public String getUsername() {
return username1;
}

public void setUsername(String username) {
this.username1 = username;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Group getGroup() {
return group;
}

public void setGroup(Group group) {
this.group = group;
}


}



Group.java


package com.bjsxt.struts;

public class Group {

private String name;

public String getName() {
return name;
}

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



EmptyPresentTestAction.java


package com.bjsxt.struts;

import java.util.ArrayList;

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;

/**
* 测试empty,notEmpty,present,notPresent
* @author Administrator
*
*/
public class EmptyPresentTestAction extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("attr1", null);
request.setAttribute("attr2", "");
request.setAttribute("attr3", new ArrayList());
return mapping.findForward("success");
}

}


IterateTestAction.java


package com.bjsxt.struts;

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;

/**
* 测试Iterate
* @author Administrator
*
*/
public class IterateTestAction extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Group group = new Group();
group.setName("尚学堂");

List userList = new ArrayList();
for (int i=0; i<10; i++) {
User user = new User();
user.setUsername("user_" + i);
user.setAge(18+i);
user.setGroup(group);
userList.add(user);
}

request.setAttribute("userlist", userList);

return mapping.findForward("success");
}

}


beanwrite.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>测试BeanWrite</title>
</head>
<body>
<h1>测试BeanWrite</h1>
<hr>
<li>普通字符串</li><br>
hello(jsp脚本):<%=request.getAttribute("hello") %><br>
hello(标签):<bean:write name="hello"/><br>
<p>
<li>html文本</li><br>
bj(default):<bean:write name="bj"/><br>
bj(filter="true"):<bean:write name="bj" filter="true"/><br>
bj(filter="false"):<bean:write name="bj" filter="false"/><br>
<p>
<li>格式化日期</li><br>
today(default):<bean:write name="today"/><br>
today(format="yyyy-MM-dd HH:mm:ss"):<bean:write name="today" format="yyyy-MM-dd HH:mm:ss"/>
<p>
<li>格式化数字</li><br>
n(default):<bean:write name="n"/><br>
n(format="###,###.####"):<bean:write name="n" format="###,###.####"/><br>
n(format="###,###.####"):<bean:write name="n" format="###,###.0000"/><br>
<p>
<li>结构</li><br>
姓名:<input type="text" value="<bean:write name="user" property="username"/>"><br>
年龄:<input type="text" value="<bean:write name="user" property="age"/>"><br>
所属组:<input type="text" value="<bean:write name="user" property="group.name"/>"><br>
</body>
</html>



iterate.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>
<%@ page import="java.util.*" %>
<%@ page import="com.bjsxt.struts.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>测试Iterate</title>
</head>
<body>
<h1>测试Iterate</h1>
<hr>
<li>jsp脚本</li><br>
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<%
List userList = (List)request.getAttribute("userlist");
if (userList == null || userList.size() == 0) {
%>
<tr>
<td colspan="3">没有符合条件的数据!</td>
</tr>
<%
}else {
for (Iterator iter=userList.iterator(); iter.hasNext(); ) {
User user = (User)iter.next();
%>
<tr>
<td><%=user.getUsername() %></td>
<td><%=user.getAge() %></td>
<td><%=user.getGroup().getName() %></td>
</tr>
<%
}
}
%>
</table>

<p>
<li>标签</li><br>
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<logic:empty name="userlist">
<tr>
<td colspan="3">没有符合条件的数据!</td>
</tr>
</logic:empty>
<logic:notEmpty name="userlist">
<logic:iterate id="u" name="userlist">
<tr>
<td>
<bean:write name="u" property="username"/>
</td>
<td>
<bean:write name="u" property="age"/>
</td>
<td>
<bean:write name="u" property="group.name"/>
</td>
</tr>
</logic:iterate>
</logic:notEmpty>
</table>
</body>
</html>


emptypresent.jsp


<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>测试empty,notEmpty,present,notPresent</title>
</head>
<body>
<h1>测试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>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值