入门struts2.0

时间:2022-03-23 10:45:19

框架是什么?

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.文件不需要修改

 package com.obtk.struts;

 import java.util.Map;

 import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext; /**
* 计算两个叔的结果
* */
public class add implements Action {
private int x; // 第一个数
private int y; // 第二个数
private int cunot; // 结果
private String op; // 方法 public String execute() throws Exception {
String reslt = "defeated";
// 如果op等于add,那么就是相加
if (this.getOp().equals("add")) {
this.setCunot(this.getX() + this.getY());
reslt = "successs";
}
// 保存放map里面。作用域是request
Map<String, Object> request = (Map) ActionContext.getContext().get(
"request");
request.put("count", getCunot());
return reslt;
} public int getX() {
return x;
} public void setX(int x) {
this.x = x;
} public int getY() {
return y;
} public void setY(int y) {
this.y = y;
} public String getOp() {
return op;
} public void setOp(String op) {
this.op = op;
} public int getCunot() {
return cunot;
} public void setCunot(int cunot) {
this.cunot = cunot;
} }

实现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的代码

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>结果页面</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">
</head>
<body>
计算结果 ${param.x }+${param.y }=${count}
</body>
</html>

successs.jsp页面代码

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

入门struts2.0

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

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