Struts中的对象传到前台,一般都会用:
request.setAttribute("modifyAccount", modifyAccount);
整体代码如下:
public ActionForward toUpdateAccount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception { User user = (User)getUser(); if(user==null){ throw new NotLoginException(); } int accountId = StringUtil.getIntValue(request.getParameter("accountId"),0); Account modifyAccount = accountService.find(accountId); if(modifyAccount==null){ this.renderText(response, "false,该账户已被删除。"); return null; } bindForm(form,modifyAccount); request.setAttribute("modifyAccount", modifyAccount); return mapping.findForward("modifyAccount"); }
要想在前台jsp页面上获得这个modifyAccount对象,有几种方式(这个jsp,是在struts的配置文件中配置好的对应mapping.findForward("modifyAccount")的jsp文件):
1.可直接这样写:
${(empty modifyAccount )?"新建账号":"修改账号" }
可直接引用request.setAttribute("modifyAccount", modifyAccount);中的"modifyAccount",注意必须同名
2.可以先在jsp文件的开头引入java代码,获得对象,然后在jsp文件里面这样使用:<%=account.userId %>
先使用java代码获得对象:
<%@page import="com.autonavi.monitor.model.Account;"%> <% Account account = (Account)request.getAttribute("modifyAccount"); %>
这样在下面直接这样使用就行了:
<input type="text" id="department" name="department" value="<%=account.getDepartment() %>" />
注意value的值一定要加上<%= %>
3.也可以这样写,可直接获得对象的值,但要注意property的值必须跟对象的属生名一致:
<html:text property="userName" styleId="userName" ></html:text>
4.Struts1标签使用方法:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>struts1标签</title> </head> <body> <html:form action="output"> <font color="blue">html:text标签:</font><br/> name属性为默认form:<html:text property="username"/>name属性非当前form:<html:text property="teacherName" name="grade" /><hr/> <font color="blue">html:select标签:</font><hr/> optionsCollection: <html:select property="username"> <html:optionsCollection name="userList" label="username" value="userID"/> </html:select> options: <html:select property="username"> <html:options collection="userList" property="userID" labelProperty="username"/> </html:select> </html:form><hr/> <font color="blue">logic:empty标签:</font><hr/> logic:empty:<logic:empty name="grade" scope="request"> <span onclick="javascript:alert('request中不存在班级!')"></span> </logic:empty> logic:notEmpty:<logic:notEmpty name="grade" scope="session"> <span onclick="alert('session中存在班级')"></span> </logic:notEmpty><hr/> <font color="blue">logic:equals标签:</font><br/> <c:forEach items="${requestScope.userList}" var="usr"> ${usr.username } <logic:equal name="usr" property="userID" value="${sessionScope.currUser.userID}"> <a href="#">编辑个人信息</a> </logic:equal> <br/> </c:forEach><br/> <font color="blue">logic:iterate标签:</font><br/> <logic:iterate id="usr" name="userList"> ${usr.username } <c:out value="${usr.birth}"></c:out><br/> </logic:iterate><hr/> <font color="blue">bean:wirte标签:</font><br/> <logic:iterate id="usr" name="userList"> <bean:write name="usr" property="username"/> <bean:write name="usr" property="score" format="#,###"/> <bean:write name="usr" property="birth" format="yyyy年MM月dd日"/><br/> </logic:iterate><hr/> <font color="blue">bean:define标签:</font><br/> 传统获取记录条数: <%=((java.util.List)request.getAttribute("userList")).size() %><br/> bean标签获取记录条数: <bean:define scope="request" id="dataList" name="userList" type="java.util.List" /> <%=dataList.size() %><hr/> <font color="blue">bean:size标签:</font><hr/> bean标签获取记录条数: <bean:size id="count" name="userList"/> ${count } <pre> User对象的字段有: private int userID; private String username; private Date birth; private Double score; Teacher对象的字段有: private int graID; private String gradeName; private String teacherName; //以下是在跳转到当前页面之前的action处理方法: public ActionForward doShow(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { System.out.println("到达action"); SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); UserForm userForm = (UserForm) form; userForm.setUserID(22); userForm.setUsername("TZ"); try { userForm.setBirth(fmt.parse("2004-5-5")); } catch (ParseException ex1) { ex1.printStackTrace(); } List<User> list = new ArrayList<User>(); try { list.add(new User(1, "Urey", fmt.parse("2009-10-1"),330.0)); list.add(new User(2, "Protoss", fmt.parse("2010-1-1"),11110.0)); list.add(new User(3, "BlackWinter", fmt.parse("2008-5-5"),3444.0)); request.setAttribute("userList", list); request.getSession().setAttribute("currUser", new User(2, "Protoss", fmt.parse("2010-1-1"),4050.0)); Grade grade = new Grade(1, "T64", "Miss Gao"); request.getSession().setAttribute("grade", grade); } catch (ParseException ex) { ex.printStackTrace(); } return mapping.findForward("ok");//在配置文件中设置的"ok"转向为当前页面 } </pre> </body> </html>
修改账户的jsp文件:
<%@ page contentType="text/html;charset=utf-8"%> <%@page import="com.autonavi.monitor.model.Account;"%> <%@ include file="/pages/common/global.jsp"%> <% Account account = (Account)request.getAttribute("modifyAccount"); %> <html:form action="/accountAction.do?method=saveModifyAccount"> <table class="form_t"> <tr> <th style="text-align:left;" class="tablelogo" colspan="2"> ${(empty modifyAccount )?"新建账号":"修改账号" } <html:hidden property="accountId"/> </th> </tr> <tr> <td style="text-align:right;"> 职工编号 </td> <td><span class="square">|</span> <html:text property="userId" styleId="userId" readonly="true"></html:text> </td> </tr> <tr> <td style="text-align:right;"> 职工姓名 </td> <td><span class="square">|</span> <html:text property="userName" styleId="userName" readonly="true"></html:text> </td> </tr> <tr> <td style="text-align:right;"> 部 门 </td> <td><span class="square">|</span> <input type="text" id="department" name="department" readonly="true" value="<%=account.getDepartment() %>" /> </td> </tr> <tr> <td style="text-align:right;"> 申请人 </td> <td><span class="square">|</span> <html:text property="applicant" styleId="applicant" readonly="true"></html:text> </td> </tr> <tr> <td style="text-align:right;"> 申请原因 </td> <td><span class="square">|</span> <html:text property="reason" styleId="reason"></html:text> </td> </tr> <tr> <td style="text-align:right;"> 账号启用时间 </td> <td><span class="square">|</span> <html:text property="startTime" styleId="startTime" readonly="true"></html:text> </td> </tr> <tr> <td style="text-align:right;"> 账号停用时间 </td> <td><span class="square">|</span> <input type="text" name="endDate_s" id="endDate_s" class="Wdate" style="cursor:pointer;" value="<%=account.getStopTime() %>" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'%y-%M-%d %H:%m:%s'})" /> </td> </tr> <tr> <td> </td><td> <input type="button" value="保存" onclick="javascript:saveAccount(this.form,'actionDiv');" class="buttonC" style="cursor:pointer;"/> <input type="button" onclick="javascript:retrieveURL('${contextPath}/accountAction.do?method=accountList','actionDiv');$('#chance_search_div').show();" value="取消" class="buttonC" style="cursor:pointer;"/> </td> </tr> <tr><td></td><td></td></tr> </table> </html:form> <script><!-- function saveAccount(thisForm,actionDiv){ if($("#startDate_s").val()==""){ alert("请选择账号启用时间。"); $("#startDate_s").val(""); $("#startDate_s").focus(); return false; } if($("#endDate_s").val()==""){ alert("请选择账号停用时间。"); $("#endDate_s").val(""); $("#endDate_s").focus(); return false; } submitForm(thisForm,"",afterSaveAccount); } function afterSaveAccount(content){ if(content!=null&&content!=""){ var arr = content.split(","); alert(arr[1]); if(arr[0]=="true"){ $("#chance_search_div").show(); retrieveURL("${contextPath}/accountAction.do?method=accountList","actionDiv"); } } } function checkUserId(url,thisObj){ url += thisObj.value; retrieveURL(url,null,null,afterCheckUserId); } function afterCheckUserId(content){ if(content!=null&&content!=""){ var arr = content.split(","); if(arr[0]=="false"){ alert(arr[1]); $("#userId_s").val(""); $("#userId_s").focus(); } } } --></script>