码迷,mamicode.com
首页 > 其他好文 > 详细

struts 配置过程 -一个计算器程序

时间:2016-08-20 11:25:49      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

1、新建一个java web项目

2、引入jar包、jar包路径:struts-1.2.9-bin\lib

3、配置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>


  <!-- Standard Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>
 <url-pattern>*.action</url-pattern>  是拦截前台配置信息。

 4、配置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="calForma" type="com.bjpowrnode.struts.CalActionForm"/>
	</form-beans>
	
	<action-mappings>
		<action path="/cala"
		        type="com.bjpowrnode.struts.CalAction"
		        name="calForma"
		        scope="request"
		 >
			 <forward name="success" path="/success.jsp"/>
			 <forward name="error" path="/error.jsp"/>
	     </action>
	</action-mappings>
	
</struts-config>
<form-bean name="calForma" type="com.bjpowrnode.struts.CalActionForm"/>  其中的name“calForm”是给文件中的数据起了个别名,在下面用( name="calForma")
通过forward放回的页面中,也中通过这里的 name="calForma"来取值的。
  type="com.bjpowrnode.struts.CalAction"  数据的来源
  scope="request" 数据提交的方式  默认是session存储。

input.jsp 页面 :显示计算机页面
技术分享
<%@ 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>
    <h>简易计算机</h>
    <hr>
    <form action="cala.action" method="post">
        <input type="text" name="value1"><br>
        <select name="flag">
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="/">/</option>
        </select>
        
        <input type="text" name="value2">
        <input type="submit" value="计算">
    </form>
</body>
</html>
View Code

  CalActionForm类: 相对于实体

技术分享
package com.bjpowrnode.struts;

import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class CalActionForm extends ActionForm {

    private int value1;
    private String flag;
    private int value2;
    
    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;
    }
    
    
}
View Code

 CalAction类: 处理业务逻辑

技术分享
package com.bjpowrnode.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;
            }
            
            //把result 放进request 中方便el表达式取值
            request.setAttribute("result", result);
            return mapping.findForward("success");
        }catch(Exception e){
            e.printStackTrace();
        }        
        return mapping.findForward("error");
        }

}
View Code

其中的 mapping.findForward("success"); 也就是 为struts-config.xml提供转向页面的依据。

success.jsp 页面:计算成功显示数据

技术分享
<%@ 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>
   ${calForma.value1}
   ${calForma.flag}
   ${calForma.value2}
   =
   ${result}
</body>
</html>
View Code

用到了EL表达式。

error.jsp页面:计算失败

技术分享
<%@ 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>
View Code

 

页面整体效果图:

技术分享

 

总结:经常用到的知识,不要因为简单都不总结。用非常短的时间坐下总结,以后在用到的时候有据可依!

 

struts 配置过程 -一个计算器程序

标签:

原文地址:http://www.cnblogs.com/yinweitao/p/5789966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!