各位大大,晚上好。
小弟对最近利用struts在做个小demo,今天碰到个如标题所示的问题,求助各位解决,不甚感激!
另外代码写得不规范的地方也希望指出,在这里谢谢了!问题描述:LoginAction.java 中request.setAttribute("Date",Today);的Date需要在MainList.jsp中获取,而在struts-config.xml中对应页面为StudentMainFrame.jsp,StudentMainFrame.jsp利用frameset集成了三页面,那我可爱的Date,你现在在哪?以下是codeGetDateDao.java
package com.tb.struts.Login;import java.util.Calendar;public class GetDateDao {

/**
 * @return  系统当前时间,type=String   yyyy-mm-dd
 */
public String GetTodayDate() {
Calendar now = Calendar.getInstance();
String time = now.get(Calendar.YEAR) + "-"
+ (now.get(Calendar.MONTH) + 1) + "-"
+ now.get(Calendar.DAY_OF_MONTH);
return time;

}}
LoginAction.javapackage com.tb.struts.Login;import java.util.Calendar;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 { String username = request.getParameter("Username");

GetDateDao dao = new GetDateDao();
String Today=dao.GetTodayDate();

if (username.trim().equals("Student")) {
request.setAttribute("Date",Today); return mapping.findForward("student");
}
if (username.trim().equals("Teacher")) {
return mapping.findForward("teacher");
}
if (username.trim().equals("Admin")) {
return mapping.findForward("admin");
} else {
request.setAttribute("ErrorInfo", "找不到对应职位");
return mapping.findForward("login");
}
}}
struts-config.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/Login" type="com.tb.struts.Login.LoginAction">
<forward name="student"
path="/student/StudentMainFrame.jsp">
</forward>

<forward name="teacher"
path="/MainTeacher/MainTeacherMainFrame.jsp">
</forward>
<forward name="admin"
path="/EquipmentRoomAdmin/EquipmentRoomAdminMainFrame.jsp">
</forward>
<forward name="login" path="/Login/Login.jsp"></forward>
</action> <action path="/student" scope="request" parameter="method"
type="com.tb.struts.Student.StudentAction">
<forward name="toCourse"
path="/student/MainFrame/MainList.jsp">
</forward>
</action>

<action path="/MainTeacher" scope="request" parameter="method"
type="com.tb.struts.MainTeacher.MainTeacherAction">
<forward name="toCourse"
path="/MainTeacher/MainFrame/MainList.jsp">
</forward>
</action>

<action path="/EquipmentRoomAdmin" scope="request" parameter="method"
type="com.tb.struts.EquipmentRoomAdmin.EquipmentRoomAdminAction">
<forward name="toCourse"
path="/EquipmentRoomAdmin/MainFrame/MainList.jsp">
</forward>
</action>

</action-mappings> <message-resources parameter="com.tb.struts.ApplicationResources" />
</struts-config>StudentMainFrame.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>操作</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="styles.css">
--> </head> <frameset rows="10%,90%">
<frame noresize="noresize" src="PublicJsp/TopFrame/TopFrame.jsp" frameborder="1"
scrolling="auto" name="TopFrame"> <frameset cols="20%,80%">
<frame noresize="noresize" src="student/LeftFrame/memu.jsp" frameborder="1"
scrolling="auto" name="LeftFrame">
<frame noresize="noresize" src="student/MainFrame/MainList.jsp" frameborder="1"
scrolling="auto" name="MainFrame">
</frameset>
<noframes>
<body>
<p>
对不起,您的浏览器不支持“帧”!
</p>
</body>
</noframes>
</frameset>
</html>MainList.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>界面</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="styles.css">
--> </head>
<script language=javascript src="JavaScript/Date.js">

</script> <script language=javascript>
function DateInfo(){
var date=document.getElementsByName("Date");
document.getElementById("today").innerHTML=date[0].value;
return date[0].value;
}
</script>
<body bgcolor="#CCCCCC">
<form action="student.do">
<input type="hidden" name="method" value="FindCourseByDate"> <div>
<p align="center">
<strong>个人课表</strong>
</p>
</div>
<br>
<br>
&nbsp;日期:
<input onfocus="calendar()" name="Date" type="text" />
<input type="submit" name="submit" value="查询" />
<br>
<table width="100%" border="1">
<tr>
<td width="10%" height="40">
<div align="center">
课次
</div>
</td>
<td width="30%">
<div align="center">
<script type="text/javascript">tomorrow(${Date})</script>
</div>
</td>
<td width="30%">
<div align="center">
${Date }
</div>
</td>
<td width="30%">
<div align="center" id="today">
<script type="text/javascript">yesterday(${Date})</script>
</div>
</td>
</tr>
<tr>
<td height="50">
<div align="center">
1-2节
</div>
</td>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
</tr>
<tr>
<td height="50">
<div align="center">
3-4节
</div>
</td>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
</tr>
<tr>
<td height="50">
<div align="center">
5-6节
</div>
</td>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
</tr>
<tr>
<td height="50">
<div align="center">
7-8节
</div>
</td>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
</tr>
</table>
</form>
</body>
</html>