基于struts1的网页版简单计算器的设计

来源:互联网 发布:ubuntu命令怎么打开 编辑:程序博客网 时间:2024/06/06 12:21

首先配置struts-Config.xml文件

<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"><struts-config>  <form-beans>     <form-bean name="calForm" type="com.bjpowernode.struts.CalActionForm"/>  </form-beans>  <action-mappings>     <action path="/cal"             type="com.bjpowernode.struts.CalAction"              name="calForm"             scope="request"           >         <forward name="success" path="/success.jsp"/>          <forward name="error" path="/error.jsp"/>     </action>     <action path=""></action>  </action-mappings></struts-config>

再进行web.xml文件的配置代码如下:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 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_2_5.xsd">    <servlet>    <servlet-name>action</servlet-name>    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>    <load-on-startup>2</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>action</servlet-name>    <url-pattern>*.action</url-pattern>  </servlet-mapping></web-app>

编写欢迎界面

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</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>    <a href="input.jsp">计算器</a>  </body></html>

编写错误界面代码如下:

 <%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
   ${calForm.value1}
   ${calForm.flag}
   ${calForm.value2}
      失败!
</body>
</html>

编写计算正确界面,代码如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
   ${calForm.value1}
   ${calForm.flag}
   ${calForm.value2}
   =
   ${result }
</body>
</html>

然后写计算程序,首先编写一个名为CalActionForm的类:

package com.bjpowernode.struts;
import org.apache.struts.action.ActionForm;
@SuppressWarnings("serial")
public class CalActionForm extends ActionForm {
    
  private int value1;
  public int getValue1() {
  return value1;
 }
 public void setValue1(int value1) {
  this.value1 = value1;
 }
 public String getFlag() {
  return flag;
 }
 public void setFlag(String flag) {
  this.flag = flag;
 }
 public int getValue2() {
  return value2;
 }
 public void setValue2(int value2) {
  this.value2 = value2;
 }
 private  String flag;
  private int value2;
}

再编写Action类:

package com.bjpowernode.struts;
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 CalAction extends Action {
 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
   CalActionForm calForm=(CalActionForm)form;
   int value1=calForm.getValue1();
   String flag=calForm.getFlag();
   int value2=calForm.getValue2();
   int result=0;
   try{
     if("+".equals(flag)){
      result=value1+value2;
     }else if("-".equals(flag)){
      result=value1-value2;
     }else if("*".equals(flag)){
      result=value1*value2;
     }else if("/".equals(flag)){
      result=value1/value2;
     }
     request.setAttribute("result",result);
     return mapping.findForward("success");
      }catch(Exception e){
       e.printStackTrace();
      }
           return mapping.findForward("error");
 }
}
部署服务器,运行即可。


原创粉丝点击