叔叔

入门struts2.0

框架是什么?

1.应用程序的半成品。

2.可重用行公共的结构。

3.按一定规则组织的一组组件。

 

model2 其实并不是一种全新的概念,很对人指出model2其实正好是经典的"模型(model) - 控制器(controller)- 视图(view)"

即mvc设计模式(mvc设计模式最早是从smalltalk语言中的mvc框架抽象出来的)。对于javaweb开发人员,mvc设计模式和model2模型是等效的。

model2在很多地方还被称为"基于action的框架";

 

struts的简单配置。

工具(MyEclipse)→ 创建web工程 → 右击工程名字选到myeclipse,然后在添加,add struts.... 

创建完成后,工具会自动创建好struts.xml 还会配置好WEB-INF下的web.xml,!ps:web,xml.文件不需要修改

 

 1 package com.obtk.struts;
 2 
 3 import java.util.Map;
 4 
 5 import com.opensymphony.xwork2.Action;
 6 import com.opensymphony.xwork2.ActionContext;
 7 
 8 /**
 9  * 计算两个叔的结果
10  * */
11 public class add implements Action {
12     private int x; // 第一个数
13     private int y; // 第二个数
14     private int cunot; // 结果
15     private String op; // 方法
16 
17     public String execute() throws Exception {
18         String reslt = "defeated";
19         // 如果op等于add,那么就是相加
20         if (this.getOp().equals("add")) {
21             this.setCunot(this.getX() + this.getY());
22             reslt = "successs";
23         }
24         // 保存放map里面。作用域是request
25         Map<String, Object> request = (Map) ActionContext.getContext().get(
26                 "request");
27         request.put("count", getCunot());
28         return reslt;
29     }
30 
31     public int getX() {
32         return x;
33     }
34 
35     public void setX(int x) {
36         this.x = x;
37     }
38 
39     public int getY() {
40         return y;
41     }
42 
43     public void setY(int y) {
44         this.y = y;
45     }
46 
47     public String getOp() {
48         return op;
49     }
50 
51     public void setOp(String op) {
52         this.op = op;
53     }
54 
55     public int getCunot() {
56         return cunot;
57     }
58 
59     public void setCunot(int cunot) {
60         this.cunot = cunot;
61     }
62 
63 }
实现Action 接口
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="test" extends="struts-default" namespace="/">
        <action name="add" class="com.obtk.struts.add">
            <result name="successs">/successs.jsp</result>
        </action>
    </package>
</struts>    
配置struts.xml的代码
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4     <head>
 5         <title>结果页面</title>
 6         <meta http-equiv="pragma" content="no-cache">
 7         <meta http-equiv="cache-control" content="no-cache">
 8         <meta http-equiv="expires" content="0">
 9         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
10         <meta http-equiv="description" content="This is my page">
11     </head>
12     <body>
13         计算结果 ${param.x }+${param.y }=${count}
14     </body>
15 </html>
successs.jsp页面代码

敲完代码.在浏览器敲 http://localhost:8080/Struts1/add.action?x=10&y=20&op=add 就会显示结果,

入门struts2.0 ,今天刚刚学的,勿喷,谢谢。

                         218786602QQ群,欢迎大家一起讨论。 

 

posted on 2014-04-14 16:49  叔叔  阅读(215)  评论(0编辑  收藏  举报

导航