jsp运行错误,已疯!求大神指点
 <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" import="java.util.Map"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>网络计算器</title>
</head>
<body>
    <jsp:useBean id="calculator" class="Calculator"/>
    <jsp:setProperty property="*" name="calculator"/>
    <%
        if(calculator.validate()){
    %>
    <font color="green">运算结果:
        <jsp:setProperty property="firstNum" name="calculator"/>
        <jsp:setProperty property="secondNum" name="calculator"/>
        <jsp:setProperty property="operator" name="calculator"/>
        =<%=calculator.calculate()%>
    </font>
    <%
        }else{
            Map<String,String> errors=calculator.getErrors();
            pageContext.setAttribute("errors",errors);
        }
    %>
    <form action="" method="post">
        第一个运算数:<input type="text" name="firstNum"/>
        <font color="red">${errors.firstNum}</font><br/>
        运算符<select name="operator" style="margin-left: 100px">
                <option value="+">+</option>
                <option value="-">-</option>
                <option value="*">*</option>
                <option value="/">/</option>
            </select><br/>
        第二个运算符:<input type="text" name="secondNum"/>
        <font color="red">${errors.secondNum}</font><br/>
        <input type="submit" value="计算"/>
    </form>
</body>
</html>

这个是Calculator类
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class Calculator {
    private String firstNum;
    private String secondNum;
    private char operator;
    private Map<String,String> errors=new HashMap<String,String>();
    public String getFirstNum() {
        return firstNum;
    }
    public void setFirstNum(String firstNum) {
        this.firstNum = firstNum;
    }
    public String getSecondNum() {
        return secondNum;
    }
    public void setSecondNum(String secondNum) {
        this.secondNum = secondNum;
    }
    public char getOperator() {
        return operator;
    }
    public void setOperator(char operator) {
        this.operator = operator;
    }
    public Map<String, String> getErrors() {
        return errors;
    }
    public void setErrors(Map<String, String> errors) {
        this.errors = errors;
    }


    public String calculate(){
        BigDecimal result=null;
        BigDecimal first=new BigDecimal(firstNum);
        BigDecimal second=new BigDecimal(secondNum);
        switch(operator){
        case '+':
            result=first.add(second);
            break;
        case '-':
            result=first.subtract(second);
            break;
        case '*':
            result=first.multiply(second);
        case '/':
            if("0".equals(second)){
                throw new RuntimeException("除数不能为零");
            }
            result=first.divide(second);
            break;
        default:
            break;
        }
        return result.toString();
    }


    public boolean validate(){
        boolean flag=true;
        Pattern p=Pattern.compile("//d+");
        if(firstNum==null||"".equals(firstNum)){
            errors.put("fristNum","第一个运算数不能为空");
            flag=false;
        }else if(!p.matcher(firstNum).matches()){
            errors.put("firstNum","第一个运算数不能为空");
            flag=false;
        }
        if(secondNum==null||"".equals(firstNum)){
            errors.put("secondNum","第一个运算数不能为空");
            flag=false;
        }else if(!p.matcher(secondNum).matches()){
            errors.put("secondNum","第一个运算数不能为空");
            flag=false;
        }
        return flag;
    }
}
为什么运行就出错呢?错误信息
An error occurred at line: 10 in the jsp file: /calculator.jsp
Calculator cannot be resolved to a type
7: <title>缃戠粶璁$畻鍣?</title>
8: </head>
9: <body>
10:     <jsp:useBean id="calculator" class="Calculator"/>
11:     <jsp:setProperty property="*" name="calculator"/>
12:     <%
13:         if(calculator.validate()){

JSP文件和Calculator类都在默认目录

3个回答

没有导入你这个类吧,,,为啥我看着这么多空行,,是我没加载出来么。。。

要加一个scope属性

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!