03--struts-taglib-BeanWrite

使用struts标签的步骤如下:

    注:引包+web.xml配置舍去

(1)在struts-config.xml文件中添加如下代码

<struts-config>
	<message-resources parameter="MessageResources" />
</struts-config>

(2)拷贝MessageResources.properties文件到项目的src下

(3)使用:在使用标签的页面加入<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

 

实例如下:

(1)index.jsp文件

<body>
    <a href="beanwrite.do">测试struts标签之----BeanWrite</a>
  </body>


(2)struts-config.xml文件

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
	<action-mappings>
		<action path="/beanwrite"
				type="com.study.struts.TestBeanWriteAction"
		>
		<forward name="testbeanwrite" path="/beanwrite.jsp"></forward>
		</action>
	</action-mappings>
	
	<message-resources parameter="MessageResources" />
</struts-config>


(3)Teacher.java文件

package com.study.struts;

public class Teacher {
	
	private String tName;

	public String gettName() {
		return tName;
	}

	public void settName(String tName) {
		this.tName = tName;
	}
	
}


(4)Student.java文件

package com.study.struts;

public class Student {
	
	private int no;
	private String name;
	private Teacher teacher;
	
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}


(5)TestBeanWriteAction.java文件

public class TestBeanWriteAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			//普通的文本写入request
		    request.setAttribute("test", "welcome to the world of struts.");
		    //带样式的html文本写入request
		    request.setAttribute("ht","<font color='green'>welcome to this world.</font>");
		    //获取当前日期写入request
		    request.setAttribute("today",new Date());
		    //把一数字写入request
		    request.setAttribute("num", 1234.5678);
		    //声明一个对象写入request
		    Teacher tc=new Teacher();
		    tc.settName("胡老师");
		    Student st=new Student();
		    st.setName("小明");
		    st.setNo(1111);
		    st.setTeacher(tc);
		    request.setAttribute("student", st);
		    
		    return mapping.findForward("testbeanwrite");
	}
}


(6)beanwrite.jsp文件

<body>
    <h1>测试struts标签之-BeanWrite</h1>
    <hr/>
    普通字符串:<bean:write name="test"/><br/>
    <!-- filter属性默认为true,即过滤掉给文本加载的修饰,若要显示font等属性,必须设置为false -->
 html文本:<bean:write name="ht" filter="false"/><br/>
 日期(默认):<bean:write name="today" /><br/>
 日期(格式化):<bean:write name="today" format="yyyy-MM-dd HH:mm:ss"/><br/>
 <!-- 数字默认为原样输出:1234.5678 -->
 数字(默认):<bean:write name="num"/><br/>
 数字(格式化):<bean:write name="num" format="###,###.00000"/><br/>
 对象:<br/>
 学生姓名:<bean:write name="student" property="name"/><br/>
 学生学号:<bean:write name="student" property="no"/><br/>
 老师姓名:<bean:write name="student" property="teacher.tName"/><br/>
  </body>

该文件前期引入了

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>


(7)结果如下:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值