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 ...
随机推荐
- 转:浅谈Radius协议 -来自CSDN:http://blog.csdn.net/wangpengqi/article/details/17097221
浅谈Radius协议 2013-12-03 16:06 5791人阅读 评论(0) 收藏 举报 分类: Radius协议分析(6) 从事Radius协议开发有段时间了,小弟不怕才疏学浅,卖弄一下, ...
- 构建seajs业务模块之grunt VS spm build
在最开始,我并不知道grunt可以构建CMD模块.(以下spm指代spm build) 当时正困惑于如何用spm方便的构建业务模块,后来看到@twinstony (感谢@twinstony的分享)使用 ...
- editplus快速定位到文章头部和尾部
经常用editplus发现有时文档比较长,要查找前面的内容时得一直滚动鼠标滚轮,或者拉动右侧边栏的导航标签,很少麻烦,有没有好的方法快速定位editplus到头部和尾部呢? 其实很简单,editplu ...
- gen already exists but is not a source folder. Convert to a source folder or rename it.
异常提示: gen already exists but is not a source folder. Convert to a source folder or rename it. 错误原因 ...
- Stanford机器学习---第六讲. 怎样选择机器学习方法、系统
原文:http://blog.csdn.net/abcjennifer/article/details/7797502 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...
- Mathematica 中 Minimize函数无法找到全局最小值时的解决方法
一直使用Minimize来找到指定约束下的函数的最小值,最近发现在一个非线性函数中使用Minimize无法提供一个"全局"最小值(使用Mathematica只是用来验证算法的,所以 ...
- ZeroMQ之Push与Pull (Java)
本系列文章均转自:http://blog.csdn.net/kobejayandy/article/details/20163431 在ZeroMQ中并没有绝对的服务端与客户端之分,所有的数据接收与发 ...
- NYOJ 61传纸条(一) 双线程DP问题
http://www.cnblogs.com/HpuAcmer/archive/2012/05/06/2486591.html 题目链接:http://acm.nyist.net/JudgeOnlin ...
- Linux命令之exit - 退出当前shell【返回值状态】
原文链接:http://codingstandards.iteye.com/blog/836625 (转载请注明出处) 用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前 ...
- 局域网所有机器都能连接MySQL数据库的设置命令
Sql代码: grant all privileges on *.* to root@"%" identified by 'abc' with grant option; flus ...