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

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

 

页面整体效果图:

 

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

 

posted @ 2016-08-20 11:02  像大海一样生活  阅读(270)  评论(0编辑  收藏  举报