有关struts的问题,返回页面结果不对!在线等待,以解决就结帖

proggirl 2003-07-31 03:47:48
运行结果按理说应该是输入SUNW,提交后得到信息:get current:25.00
而实际结果是:无论输入什么,都好像重置一样
我觉得可能是根本form里面得知没有传过去,请大家帮忙看一看吧!

action程序如下:

package wiley;
import java.io.IOException;
import javax.servlet.ServletException;
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 LookupAction extends Action {
protected Double getQuote(String symbol) {
if ( symbol.equalsIgnoreCase("SUNW") ) {
return new Double(25.00);
}

return null;
}
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException{
Double price = null;

// Default target to success
String target = new String("success");
if (form!=null) {
// Use the LookupForm to get the request parameters
LookupForm lookupForm = (LookupForm)form;
String symbol = lookupForm.getSymbol();
//price = new Double("11");
getQuote(symbol);
}


// Set the target to failure
if ( price ==null ) {
target = new String("failure");

}
else {
request.setAttribute("PRICE", price);
}
// Forward to the appropriate View
return (mapping.findForward(target));

}
}

formbean如下:
package wiley;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LookupForm extends ActionForm {
private String symbol= null;
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public void reset(ActionMapping mapping,
HttpServletRequest request) {
this.symbol = null;
}
}

struts-config.xml如下:
<?xml version="1.0" encoding="ISO-8859-1" ?>

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

<!--
This is the Struts configuration file for the example application,
using the proposed new syntax.
-->
<struts-config>


<form-beans>
<form-bean name="LookupForm" type="wiley.LookupForm "/>
</form-beans>
<action-mappings>
<action path="/Lookup"
type="wiley.LookupAction"
name="LookupForm "
input="/index.jsp">
<forward name="success" path="/quote.jsp"/>
<forward name="failure" path="/index.jsp"/>
</action>
</action-mappings>
</struts-config>




...全文
22 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kui 2003-07-31
  • 打赏
  • 举报
回复
struts-config.xml文件自动添加如下内容:

<form-bean name="lyk01Form"
type="org.apache.struts.webapp.example.lyk01Form" />
</form-beans>

///////////////////////////
<action path="/lyk01Action"
type="org.apache.struts.webapp.example.lyk01Action"
name="lyk01Form"
scope="session"
input="/lyk01.jsp">
<forward name="success" path="/lyk01Out.jsp" />
</action>

kui 2003-07-31
  • 打赏
  • 举报
回复
你在quote.jsp中,为何不用<bean:write name="" property="" scope="request" filter="true" />来输出呢?
非常强烈推荐你到ftp://210.36.70.51下载JSPStudio开发工具,
在JSPStudio中,利用Struts文件向导建立struts文件:在文件目录工具栏中点击鼠标右键,然后选择“Struts文件向导”。 在Struts文件向导对话框中输入JSP文件名,系统自动产生ActionForm、Action、JSPOut文件,自动产生及更新XML配置文件。
例如只要输入表格JSP文件名为“lyk01”五个字母,自动生成文件lyk01.jsp(输入)、lyk01Out.jsp、lyk01ActionForm、lyk01Action及自动产生及更新XML配置文件。
lyk01Out.jsp
<%@ page contentType="text/html;charset=GB2312" language="java" %>
<%@ taglib uri="/WEB-INF/app.tld" prefix="app" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<html:html>
<head>
<title></title>
<html:base/>
</head>
<body bgcolor="white">
<html:form action="/lyk01Action.do" method="post">
<html:text property="mybeanvariable1" />
<html:submit value="提交" />
<html:reset value="重写" />
</html:form>
</body>
</html:html>

lyk01Out.jsp
<%@ page contentType="text/html;charset=GB2312" language="java" %>
<%@ taglib uri="/WEB-INF/app.tld" prefix="app" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<html:html>
<head>
<title></title>
<html:base/>
</head>
<body bgcolor="white">
<html:html>
<head>
<title></title>
<html:base/>
</head>
<body bgcolor="white">
<bean:write name="lyk01Form" property="mybeanvariable1" />
</body>
</html:html>
</body>
</html:html>

lyk01Form.java
package org.apache.struts.webapp.example;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public final class lyk01Form extends ActionForm
{
private String mybeanvariable1;

//myfiledata;

public String getMybeanvariable1()
{
return (this.mybeanvariable1);
}

public void setMybeanvariable1(String mybeanvariable1)
{
this.mybeanvariable1=mybeanvariable1;
}

}

lyk01Action.java
package org.apache.struts.webapp.example;

import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.ModuleException;
import org.apache.struts.util.MessageResources;

public final class lyk01Action extends Action
{
// 变量定义:
private Log log = LogFactory.getLog("org.apache.struts.webapp.Example");

//函数定义:
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Extract attributes and parameters we will need
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
HttpSession session = request.getSession();
lyk01Form myform = (lyk01Form) form;
String variable1= myform.getMybeanvariable1();
try
{
if(variable1.trim().equals(""))
{
return new ActionForward(mapping.getInput());
}
//其它代码
return (mapping.findForward("success"));
}
catch (Exception e)
{
//log.error("出错", e);
//log.trace("出错");
throw new RuntimeException(e.getMessage());
}
}
}
proggirl 2003-07-31
  • 打赏
  • 举报
回复
补充一下啊:
index.jsp

<%@ page language="java" %>
<%@ taglib
uri="/WEB-INF/struts-html.tld"
prefix="html" %>
<html>
<head>
<title>Wiley Struts Application</title>
</head>
<body>
<table width="500"
border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
</tr>
<tr bgcolor="#36566E">
<td height="68" width="48%">
<div align="left">
<img src="images/hp_logo_wiley.gif"
width="220"
height="74">
</div>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<html:form action="Lookup"
name="LookupForm"
type="wiley.LookupForm" >
<table width="45%" border="0">
<tr>
<td>Symbol:</td>
<td><html:text property="symbol" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit /></td>
</tr>
</table>
</html:form>
</body>
</html>


quote.jsp

<html>
<head>
<title>Wiley Struts Application</title>
</head>
<body>
<table width="500"
border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
</tr>
<tr bgcolor="#36566E">
<td height="68" width="48%">
<div align="left">
<img src="images/hp_logo_wiley.gif"
width="220" height="74">
</div>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>
Current Price : <%= request.getAttribute("PRICE") %>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</body>
</html>

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧