struts加法器是不是配置有问题?跳转时出错
部署运行时出现路径错误 --
严重: Invalid path was requested在跳转的时候出现问题
检查了一下路径没有错误
struts.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> <form-bean name="caculatorForm" type="org.caculatorForStruts.form.CaculatorForm"></form-bean> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings> <!-- 计算器首页 --> <action path="/caculatorIndex" type="org.caculatorForStruts.action.CaculatorAction" attribute="caculatorForm" name="caculatorForm" scope="request" validate="false" input="/caculator/error.jsp"> <forward name="caculatorIndex" path="/caculator/caculatorIndex.jsp"></forward> </action> <!-- 计算器结果 --> <action path="/result" type="org.caculatorForStruts.action.ResultAction" attribute="caculatorForm" name="caculatorForm" scope="request" validate="false" input="/caculator/error.jsp"> <forward name="result" path="/caculator/result.jsp"></forward> </action> <!-- 计算器DisPathAction --> <action path="/caculator" type="org.caculatorForStruts.action.CaculatorDisPathAction" parameter="method" name="caculatorForm" attribute="caculatorForm" validate="false"> <forward name="caculatorIndex" path="/caculator/caculatorIndex.jsp"></forward> <forward name="result" path="/caculator/result.jsp"></forward> </action> </action-mappings> <message-resources parameter="com.yourcompany.struts.ApplicationResources" /> </struts-config>
两个Action
package org.caculatorForStruts.action; 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 CaculatorAction extends Action{ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("caculatorIndex"); } }
2
package org.caculatorForStruts.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import org.caculatorForStruts.biz.Caculator; import org.caculatorForStruts.biz.imp.CaculatorImp; import org.caculatorForStruts.form.CaculatorForm; public class CaculatorDisPathAction extends DispatchAction{ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub return super.execute(mapping, form, request, response); } /** * 计算器首页 * @param mapping * @param form * @param request * @param response * @return * @throws Exception */ public ActionForward caculatorIndex(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("caculatorIndex"); } /** * 计算器结果页面 * @param mapping * @param form * @param request * @param response * @return * @throws Exception */ public ActionForward result(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { double result = 0; String oper = ""; String operator = request.getParameter("operator"); CaculatorForm caculatorForm = (CaculatorForm) form; Caculator caculator = new CaculatorImp(); if(operator.equals("jia")){ result = caculator.jia(caculatorForm); oper = "+"; } if(operator.equals("jian")){ result = caculator.jian(caculatorForm); oper = "-"; } if(operator.equals("cheng")){ result = caculator.cheng(caculatorForm); oper = "*"; } if(operator.equals("chu")){ result = caculator.chu(caculatorForm); oper = "/"; } request.setAttribute("caculatorForm", caculatorForm); request.setAttribute("result", result); request.setAttribute("operator", oper); return mapping.findForward("result"); } }
两个jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <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"> --> <script type="text/javascript"> function check(operator){ var num1 = document.getElementById("num1").value; var num2 = document.getElementById("num2").value; if(num1==""){ alert("第一个数字不能为空!"); return false; } if(num2==""){ alert("第二个数字不能为空!"); return false; } caculatorForm.action = "caculator.do?method=result&operator="+operator; caculatorForm.submit(); return true; } </script> </head> <body> <h2><b>计算器</b></h2><br> <form id="caculatorForm" name="caculatorForm" action="#" method="post"> 第一个数:<input type="text" id="num1" name="num1"/><br> 第二个数:<input type="text" id="num2" name="num2"/> <br> <input type="button" value="加" onClick="check('jia')"/> <input type="button" value="减" onClick="check('jian')"/> <input type="button" value="乘" onClick="check('cheng')"/> <input type="button" value="除" onClick="check('chu')"/> </form> </body> </html>
2
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <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> <body> <h2><b>计算结果</b></h2><br> ${caculatorForm.num1 }${operator }${caculatorForm.num2 }= ${result } <br> </body> </html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
谢谢帮忙
form包
BIZ
请将你的完整日志贴出来。
贴出来了帮忙看下啊,谢谢
请将web.xml的配置也贴出来, 另外, 你的代码中有很多可能会出错的地方, 这个东西应该是实验性质的吧?