首页 > 代码库 > struts1四:常用标签

struts1四:常用标签

struts1支持的5种标签:
HTML 标签: 用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单
Bean 标签: 在访问JavaBeans 及其属性,以及定义一个新的bean 时使用
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
Logic 标签: 管理条件产生的输出和对象集产生的循环
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
Template 标签: 随着Tiles框架包的出现,此标记已开始减少使用
Nested 标签: 增强对其他的Struts 标签的嵌套使用的能力

在Action中设置属性并在jsp中取出属性
LoginAction

package com.bjpowernode.struts;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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;


public class LoginAction extends Action{

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        LoginActionForm laf = (LoginActionForm)form;
        if("admin".equals(laf.getUsername()) && "admin".equals(laf.getPassword())){
            
            request.setAttribute("message", "test message!");
            request.setAttribute("user", createUser("张三", "123456", 29));
            request.setAttribute("userList", createUserList());
            request.setAttribute("userMap", createUserMap());
            return mapping.findForward("success");
        }
        return mapping.findForward("error");
    }

    private User createUser(String username, String password, int age){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setAge(age);
        user.setCreateDate(new Date());
        return user;
    }
    
    private List<User> createUserList(){
        List<User> userList = new ArrayList<User>();
        userList.add(createUser("李四", "123123", 30));
        userList.add(createUser("王五", "123999", 34));
        userList.add(createUser("赵六", "123123", 39));
        return userList;
    }
    
    private Map<String, User> createUserMap(){
        Map<String, User> map = new HashMap<String, User>();
        map.put("k1", createUser("map1", "map123", 100));
        map.put("k2", createUser("map2", "map12123", 110));
        map.put("k3", createUser("map3", "map123map", 130));
        return map;
    }
}

 

跳转后的JSP
login_success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP ‘login_success.jsp‘ starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">
    -->

  </head>
  
  <body>
    This is my JSP page. Success!!! <br>
    <!-- 普通属性取出 -->
    <bean:write name="message"/><br>
    <!-- 对象类型的属性取出 -->
    <bean:write name="user" property="username"/><br>
    <bean:write name="user" property="password"/><br>
    <bean:write name="user" property="createDate" format="yyyy-MM-dd HH:mm:ss"/><br>
    <hr>
    <!-- List类型的属性取出 -->
    <logic:iterate id="user" name="userList">
        <bean:write name="user" property="username"/><br>
        <bean:write name="user" property="password"/><br>
        <bean:write name="user" property="createDate" format="yyyy-MM-dd HH:mm:ss"/><br>
    </logic:iterate>
    <hr>
    <!-- Map类型的属性取出 -->
    <logic:iterate id="userIteam" name="userMap">
        <bean:write name="userIteam" property="key"/><br>
        <bean:write name="userIteam" property="value.username"/><br>
        <bean:write name="userIteam" property="value.password"/><br>
        <bean:write name="userIteam" property="value.createDate" format="yyyy-MM-dd HH:mm:ss"/><br>
    </logic:iterate>
  </body>
</html>

 

struts1四:常用标签