Java Hour 32 Weather ( 5 ) struts2 – Action class
有句名言,叫做10000小时成为某一个领域的专家。姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧。
Hour 32
Struts2 Action
1 将action 映射到 action class
2 将action class 返回的结果 映射到一个 view
3 写action class 的控制逻辑
所以这里关键点是action class
Action Class
public class HelloWorldAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private static int helloCount = 0;
public int getHelloCount() {
return helloCount;
}
public void setHelloCount(int helloCount) {
HelloWorldAction.helloCount = helloCount;
}
private MessageStore messageStore;
public String execute() throws Exception {
messageStore = new MessageStore();
helloCount++;
return SUCCESS;
}
这里一般直接继承ActionSupport 基类。
然后override 默认的execute 方法。
注意这里可能会抛出Exception 的异常,关于异常将在后面讲解。
只要在Action Class 里增加符合约定的属性和字段,Struts2 将自动赋值到该属性。
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 String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
private static int helloCount = 0;
public int getHelloCount() {
return helloCount;
}
public void setHelloCount(int helloCount) {
HelloWorldAction.helloCount = helloCount;
}
private MessageStore messageStore;
public String execute() throws Exception {
messageStore = new MessageStore();
helloCount++;
if (userName != null) {
messageStore.setMessage(messageStore.getMessage() + " " + userName);
}
return SUCCESS;
}
public MessageStore getMessageStore() {
return messageStore;
}
public void setMessageStore(MessageStore messageStore) {
this.messageStore = messageStore;
}
}

随着方法和属性的增多,这个顺序有点乱。
eclipse 有各种字段,方法,自动分类排序功能么?
这个小时就暂时到这里为止,明天继续。
综合以上这些部分,解决我们这个weather 项目应该不成问题了。
不过不得不说,这个问题写得挺nice 的,适合我这样的初学者。
http://struts.apache.org/release/2.1.x/docs/processing-forms.html
Java Hour 32 Weather ( 5 ) struts2 – Action class的更多相关文章
- Java Hour 35 Weather ( 8 ) struts2 – message resource
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 35 刚发表了一条闪存,在这个公司快满3个月了,该正式决定留下来还是 ...
- Java Hour 34 Weather ( 7 ) struts2 – validate
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 34 Form Validation 一般Form 提交都有验证的, ...
- Java Hour 36 Weathre ( 9 ) struts2 – exception
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 35 Exception Handling 直接添加全局性的异常处理 ...
- struts2 java.lang.StackOverflowError org.apache.struts2.json.JSONWriter
1. 问题描述: 页面通过异步访问action, action的方法通过map封装数据,struts的result的type设置为json,后台报错 六月 25, 2016 6:54:33 下午 ...
- Struts2—Action
二.命名空间namespace ·命名空间namespace须要放在相应的package下 ·Namespace必须以"/开头". ·Result的name是"succe ...
- Java后台处理框架之struts2学习总结
Java后台处理框架之struts2学习总结 最近我在网上了解到,在实际的开发项目中struts2的使用率在不断降低,取而代之的是springMVC.可能有很多的朋友看到这里就会说,那还不如不学str ...
- java.lang.NullPointerException org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
采用SSH框架时出现了 java.lang.NullPointerException org.apache.struts2.impl.StrutsActionProxy.getErrorMessage ...
- struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
- struts2 action 页面跳转
struts2 action 页面跳转 标签: actionstruts2redirect 2013-11-06 16:22 20148人阅读 评论(0) 收藏 举报 (1)type="di ...
随机推荐
- https://github.com/yrs244742688/GeneratePemWithMoAndEx RSA加密
<RSAKeyValue> <Modulus> xA7SEU+e0yQH5rm9kbCDN9o3aPIo7HbP7tX6WOocLZAtNfyxSZDU16ksL6 Wjuba ...
- [Angularjs]ng-switch用法
用法描述 ng-switch根据表达式的值显示或这隐藏对应部分.类似c#或者其他预览里面的switch用法.可以慢慢体会. 说道ng-switch就要说到子元素该怎么根据当前值进行变化.子元素可以通过 ...
- HttpWebRequest后台读取网页类
using System;using System.Linq;using System.Collections.Generic;using System.Web;using System.Config ...
- 转<<C#集合Dictionary中按值的降序排列
转载地址:http://blog.sina.com.cn/s/blog_5c5bc9070100pped.html C#集合Dictionary中按值的降序排列 static void Main(st ...
- 微信公众平台回复过了怎么不能再次回复?亲们要注意查看"公众平台回复用户消息时限变更通知"的公告啊
有网友一直在问“微信公众平台回复过了怎么不能再次回复?”,其实这个是微信公众平台订阅号设置的一个时间限制,一般都会提示:由于该用户48小时未与你互动,你不能再主动发消息给他.直到用户下次主动发消息给你 ...
- HttpWatch详解
一 概述: HttpWatch强大的网页数据分析工具.集成在Internet Explorer工具栏.包括网页摘要.Cookies管理.缓存管理.消息头发送/接受.字符查询.POST 数据和目录管理功 ...
- jsp 学习 第3步 - el 自定义方法 tld 说明
使用 el 的过程中,需要使用到后端代码处理逻辑,这个时候我们就需要自定义 方法. 如我们后端代码定义如下: package com.rhythmk.common; public class FncH ...
- [BZOJ1529][POI2005]ska Piggy banks
[BZOJ1529][POI2005]ska Piggy banks 试题描述 Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个存钱罐的钥匙放 ...
- ZeroMQ(java)中对IO的封装(StreamEngine)
哎,各种各样杂七杂八的事情...好久没有看代码了,其实要搞明白一个与IO相关的框架,最好的办法就是把它的I/0的读写两个过程搞清楚...例如在netty中,如果能将eventLoop的运行原理搞清楚, ...
- The CompilerVersion constant identifies the internal version number of the Delphi compiler.
http://delphi.wikia.com/wiki/CompilerVersion_Constant The CompilerVersion constant identifies the in ...