struts加法器是不是配置有问题?跳转时出错

发布于 2021-11-16 14:11:34 字数 7749 浏览 860 评论 5

部署运行时出现路径错误 --

严重: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾城泪 2021-11-20 01:28:54

谢谢帮忙

网名女生简单气质 2021-11-20 01:14:34

form包

package org.caculatorForStruts.form;

import org.apache.struts.action.ActionForm;

public class CaculatorForm extends ActionForm {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7533133717420346636L;
	private double num1;
	private double num2;

	public double getNum1() {
		return num1;
	}

	public void setNum1(double num1) {
		this.num1 = num1;
	}

	public double getNum2() {
		return num2;
	}

	public void setNum2(double num2) {
		this.num2 = num2;
	}

}

BIZ

package org.caculatorForStruts.biz;

import org.caculatorForStruts.form.CaculatorForm;

public interface Caculator {
  /**
   * 加法实现
   * @param caculatorForm
   * @return 
   */
  public double jia(CaculatorForm caculatorForm);
  /**
   * 减法实现
   * @param caculatorForm
   * @return 
   */
  public double jian(CaculatorForm caculatorForm);
  /**
   * 乘法实现
   * @param caculatorForm
   * @return 
   */
  public double cheng(CaculatorForm caculatorForm);
  
  /**
   * 除法实现
   * @param caculatorForm
   * @return 
   */
  public double chu(CaculatorForm caculatorForm);
  
}

package org.caculatorForStruts.biz.imp;

import org.caculatorForStruts.biz.Caculator;
import org.caculatorForStruts.form.CaculatorForm;

public class CaculatorImp implements Caculator {

	@Override
	public double jia(CaculatorForm caculatorForm) {
		return caculatorForm.getNum1() + caculatorForm.getNum2();
	}

	@Override
	public double jian(CaculatorForm caculatorForm) {
		return caculatorForm.getNum1() - caculatorForm.getNum2();
	}

	@Override
	public double cheng(CaculatorForm caculatorForm) {
		return caculatorForm.getNum1() * caculatorForm.getNum2();
	}

	@Override
	public double chu(CaculatorForm caculatorForm) {
		return caculatorForm.getNum1() / caculatorForm.getNum2();
	}

}

少女净妖师 2021-11-19 14:36:29

请将你的完整日志贴出来。

孤檠 2021-11-19 01:25:33

贴出来了帮忙看下啊,谢谢

本王不退位尔等都是臣 2021-11-18 01:24:09

请将web.xml的配置也贴出来, 另外, 你的代码中有很多可能会出错的地方, 这个东西应该是实验性质的吧?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文