struct2(二) struct2的hello world 程序
在struct2 的web应用程序中,当你点击一个超链接或者提交一个HTML页面的时候,并不是直接的转向一个另一个的页面,而是转到你提供的一个Java 类。这个过程被称为一个action,一个action执行完毕之后,选择一个资源进行响应数据,(就是返回数据,选择页面展示数据)。结果常常是一个页面,也可能是一个PDF文见,一个Excel单据,或者一个Java应用程序。
假设你想创建一个简单的用来展示欢迎信息的hello world程序,建好一个空的基础的struct2的web应用程序以后,接着创建一个hello world的例子,你需要做以下四件事情。
1. 首先创建一个类来保存欢迎信息(这个就是展示的数据,模型,model)。
2. 创建一个页面来展示数据(一般是一个html或者jsp文件,view)。
3. 创建一个action,来控制用户,模型,展示之间的交互。
4. 在structs.Xml中创建一个在action和view之间的交互。
通过这些组件,我们正在把工作分为不同的部分,符合非常有名的MVC模型,这样的分离原则使复杂的应用变得更加的简单,下面我们利用在struct2(一)中的基本的struct2的工程,编码实现上面所说的。
编码实现:
我们把基本的structTest 增加一个model 类来存储我们的欢迎信息,一个展示页面来展示我们的信息。一个action作为controller,一个配置信息把一切结合起来。
第一步:创建model Class MessageStore.java
- package org.apache.struts.helloworld.model;
- public class MessageStore {
- private String message;
- public MessageStore() {
- setMessage("Hello Struts User");
- }
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- }
在上面的model Class中的get/set方法,提供对类私有成员message的操作方法。在struct2框架中要求view(jsp或者html页面)的展示的数据必须遵循 JavaBean-style conventions.原则。
第二部: 创建action 类: HelloWorldAction.java
我们需要一个action class 来作为controller。这个action类用来响应用户的操作(在这个例子中就是点击html页面的一个超链接,发送给servlet 容器一个特殊的URL),然后action 类中执行完毕以后,会返回一个String类型的信息,在这个结果行的信息的基础上,一个特殊的展示页面(在这个例子中为HelloWorld.jsp)响应了。
具体的代码:
- package org.apache.struts.helloworld.action;
- import org.apache.struts.helloworld.model.MessageStore;
- import com.opensymphony.xwork2.ActionSupport;
- public class HelloWorldAction extends ActionSupport {
- private static final long serialVersionUID = 1L;
- private MessageStore messageStore;
- @Override
- public String execute() throws Exception {
- messageStore = new MessageStore() ;
- return SUCCESS;
- }
- public MessageStore getMessageStore() {
- return messageStore;
- }
- public void setMessageStore(MessageStore messageStore) {
- this.messageStore = messageStore;
- }
- }
struct2 框架将会创建一个HelloWorldAction 类的实例,调用其中的execute 方法,来响应用户的操作(在这个例子中就是点击html页面的一个超链接,发送给servlet 容器一个特殊的URL)。
在这个例子中,执行的方法将会创建MessageStore实例,然后返回SUCCESS 这个常量字符串。
MessageStore实例的get/set 方法提供访问类私有成员的途径,所以我们可以知道在展示页面(HelloWorld.jsp)对MessageStore实例是可见的,当然我们需要MessageStore符合 JavaBean-style 规范。
第三步:创建展示页面 HelloWorld.jsp
我们需要一个页面来展示model MessageStore中存储的信息。
HelloWorld.jsp 可以放在WebRootFile 文件下,源码:
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <!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=ISO-8859-1">
- <title>Hello World!</title>
- </head>
- <body>
- <h2><s:property value="messageStore.message" /></h2>
- </body>
- </html>
标签指令告诉servlet容器,这个页面使用Struct2 标签库,这些标签会在前面放上一个s
<s:property>
标签展示的是:HelloWorldAction
的方法getMessageStore
返回的 MessageStore
实例,.maessge 的意思是告诉 Struct2框架调用
MessageStore
的getMessage方法,这个方法返回一个String类型的欢迎数据,将会被展示。
第四步:在strcuts.xml 中创建Struct 的配置信息
我们需要有一个映射,把URL 和
HelloWorldAction,以及HelloWorld.jsp 关联在一起。
struts.xml 源码:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.devMode" value="true" />
- <package name="basicstruts2" extends="struts-default">
- <action name="index">
- <result>/index.jsp</result>
- </action>
- <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
- <result name="success">/HelloWorld.jsp</result>
- </action>
- </package>
- </struts>
第五步:创建URL action
既是创建对应Action的URL,也就是创建用户操作的入口。此例中我们使用我们原来的index.jsp,
在其中加入超链接。
index.jsp 源码:
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <!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=ISO-8859-1">
- <title>Basic Struts 2 Application - Welcome</title>
- </head>
- <body>
- <h1>Welcome To Struts 2!</h1>
- <p><a href="<s:url action='hello'/>">Hello World</a></p>
- </body>
- </html>
Struts url 标签创建了一个Hello Action的URL 这个Action 会映射到 HelloWorldAction 类上面以及他的执行方法。当用户点击这个URL的时候,Struct2的框架就会执行HelloWorldAction 的execute方法,得到相应的返回值,view页面(HelloWorld.jsp)展示出来响应的信息。
第六步:测试(Build the WAR File and Run The Application)
我们使用的是Jetty 所以和使用Tomcat,以及mvn打包的不同,直接debug JettyStarter 启动监听127.0.0.1:12345,在浏览器中输入:127.0.0.1:12345/struct2test/index.action
点击 Hello World
代码运行的过程:
1. 浏览器向服务器发送URL:http://127.0.0.1:12345/struct2test/hello.action
2. servlet 容器接受到hello.action的请求,根据web.xml的设置,容器会把这个请求交给
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
StructsPrepareAndExecuteFilter 是整个框架的入口。
3.框架会寻找一个与hello相对应的Action,这个在structs.xml中已有设置,这个Action对应的类也会找到:HelloWorldAction 然后会实例化HelloWorldAction,并调用它的execute方法。
4. excutor执行,创建MessageStore 实例,并且返回SUCCESS框架会检查映射,看看SuCCESS会被返回到那个表现页面,框架会告诉容器把信息作为返回的数据提供给HelloWorld.jsp
.
5. HelloWorld.jsp展示的过程在前面已经说了,不再重复。
struct2(二) struct2的hello world 程序的更多相关文章
- 微信小程序 - 配置普通二维码跳小程序
普通二维码跳小程序规则: https://developers.weixin.qq.com/miniprogram/introduction/qrcode.html#%E5%8A%9F%E8%83%B ...
- MyBatis系列二 之 数据库列名于程序实体类中字段名称不一致
MyBatis系列二 之 数据库列名于程序实体类中字段名称不一致 情景:当数据库中的列名与我们程序实体类中的字段名称不一致 使用ResultMap节点配置信息 在映射文件中 ...
- 微信小程序扫描普通二维码打开小程序的方法
很久没有写博客了,之前换了一份工作,很久没有做Android开发了,现在转做前端开发了,记录一下遇到的问题及解决的方法. 最近做微信小程序开发,遇到一个需求,后台管理系统生成的问卷和投票会有一个二维码 ...
- 学习ASP.NET Core Blazor编程系列二——第一个Blazor应用程序(中)
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 四.创建一个Blazor应用程序 1. 第一种创 ...
- 学习ASP.NET Core Blazor编程系列二——第一个Blazor应用程序(下)
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...
- 学习ASP.NET Core Blazor编程系列二——第一个Blazor应用程序(完)
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...
- Struct2(三) Struct2 标签
在上一篇 Struct2(二)中,我们新建了工程Struct2test用来验证hello World 程序,在index.jsp中,我们添加了一个Struct2 uri 标签用来创建一个指向hello ...
- Android学习笔记(二十一)——实战:程序数据共享
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 我们继续在Database项目的基础上继续开发,通过内容提供器来给它加入外部访问接口.首先将 MyDataba ...
- Android项目实战(二):安卓应用程序退出的三种方法
现在的APP退出的时候都不是让用户点击了“后退键”就退出.防止用户点错了后退键而造成的用户体检不好. 一年前搞的Demo代码不见了,重新写下就当是复习和以后直接拿来用把 目前流行的解决一般分为两种: ...
- 使用Graham扫描法求二维凸包的一个程序
#include <iostream> #include <cstring> #include <cstdlib> #include <cmath> # ...
随机推荐
- Storm drpc学习
示例代码: package com.lky.test; import org.apache.commons.logging.Log; import org.apache.commons.logging ...
- Android笔记(三):View一些值得注意的地方
Button android:textAllCaps="false" // Button上的英文字符不转成大写 EditText android:maxLines="2& ...
- JavaScript 去除数组重复成员
[...new Set(array)] 运用 Set结构不会添加重复的值 和...解构 function dedupe(array) { return Array.from(new Set(array ...
- UITableView的刷新
UITableView的刷新1> 数据刷新的总体步骤* 修改模型数据* 刷新表格(刷新界面) 2> 刷新表格(刷新界面)的方法* 全局刷新(每一行都会重新刷新)- (void)reload ...
- java关键字-transient
java语言的关键字,变量修饰符,如果用transient声明一个实例变量,当对象存储时,它的值不需要维持. Java的serialization提供了一种持久化对象实例的机制.当持久化对象时,可能有 ...
- hdu 2031
水题 AC代码: #include<iostream> using namespace std; int main() { int i,j,b[1000],k; while(cin> ...
- easyui 快速开发整理
下面整理了关于easyui的datagrid的开发文档,复制黏贴即刻使用 1: <link href="../../Content/easyUI/themes/default/easy ...
- Hadoop map reduce 任务数量优化
mapred.tasktracker.map.tasks.maximum 官方解释:The maximum number of map tasks that will be run simultan ...
- C#方法的重载
方法的重载 参数不同,个数可以相同 参数相同,个数不能相同 static void Main(string[] arr) { Console.WriteLine(M(,)); Console.Read ...
- itext poi 学习之旅 (1)创建pdf
从零开始学习itext 创建pdf 1.用到流进行创建的pdf import java.io.File; import java.io.FileOutputStream; import com.ite ...