今天刚学struts,交作业的题目,步骤打乱了,因为删掉了很多无关计算器的内容。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 第一步:配置filter,为启用Struts做准备 --> <filter> <filter-name>StrutsPrepareAndExecuteFilter</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>StrutsPrepareAndExecuteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> <!-- 第4步:建立struts.xml文件 ,配置struts框架的所需内容--> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="helloPackage" extends="struts-default" namespace="/"> <action name="helloRequest" class="Ex1124.HelloAction" method="helloAction"> <result name="hellook" type="dispatcher"> /ok.jsp </result> </action> <action name="indexRequest" class="Ex1124.ConfigAction" method="execute"> <result name="configok" type="dispatcher"> /index.jsp </result> </action> </package> <!-- 第9步:添加处理计算的package --> <constant name="" value="2333"></constant> <package name="calc" extends="struts-default" namespace="/"> <action name="calcexe" class="Ex1124.Calc" method="cal"> <!-- 特别注意:此处出现了两个错误,首先将method写错为类名,而不是方法名, 第二将class写成Ex1124/Calc,导致所有页面404 --> <result name="calOK" type="dispatcher"> /cal.jsp </result> </action> </package> </struts> package Ex1124; import com.opensymphony.xwork2.ActionSupport; //第8步:创建Calc,准备处理计算结果 public class Calc extends ActionSupport{ private String username; private Integer num1; private String opt; private Integer num2; private Integer result=0; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getNum1() { return num1; } public void setNum1(Integer num1) { this.num1 = num1; } public String getOpt() { return opt; } public void setOpt(String opt) { this.opt = opt; } public Integer getNum2() { return num2; } public void setNum2(Integer num2) { this.num2 = num2; } public Integer getResult() { return result; } public void setResult(Integer result) { this.result = result; } public String cal() throws Exception{ if(opt.equals("+")){ result=num1+num2; }else if(opt.equals("-")){ result=num1-num2; }else if(opt.equals("*")){ result=num1*num2; }else if(opt.equals("/")){ result=num1/num2; } return "calOK"; } } <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!-- 第10步:编写cal.jsp页面 --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>计算器</title> </head> <body> <!-- 获取文本域路径 --> <form action=${pageContext.request.contextPath}/calcexe method="post"> <table border="2" align="center"> <tr> <th>用户名</th> <td><input type="text" name="username" value="${requestScope.username}"/></td> </tr> <tr> <th>操作数一</th> <td><input type="text" name="num1" value="${requestScope.num1}"/></td> </tr> <tr> <th>运算符</th> <td> <input type="radio" name="opt" value="+" ${requestScope.opt=='+'?'checked':'checked' }/>+ <input type="radio" name="opt" value="-" ${requestScope.opt=='-'?'checked':'' }/>- <input type="radio" name="opt" value="*" ${requestScope.opt=='*'?'checked':'' }/>* <input type="radio" name="opt" value="/" ${requestScope.opt=='/'?'checked':'' }/>/ </td> </tr> <tr> <th>操作数二</th> <td><input type="text" name="num2" value="${requestScope.num2}"/></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="计算" action=""/></td> </tr> <tr> <td colspan="2" align="center"><input type="text" name="result" value="${requestScope.result}"/></td> </tr> </table> </form> </body> </html>
summer_last 2016/03/03 21:59
不错支持一下
admin 博主 2016/03/03 23:29
@ 谢谢