package com.huawei.sdtrp.command;
import java.util.logging.Logger;
/**
* <pre>
* 命令解析器,默认实现
* 方法一:
* 使用ClassLorder来加载(包名+CMD+"Command")命令类路径
* 具有包名限制
* 方法二:
* 在实际Action中,采用Spring注入包名称属性,再使用命令解析器实例化,具体的命令对象(可行,但是设计不太合理[不是Action要做的事情])
* 方法三:
* 有没有另外的方式,来构造命令对象???(只知道命令字符串)
* </pre>
* @author cWX194449
*
*/
public class CommandParserImpl implements CommandParser {
private static Logger log = Logger.getLogger(CommandParserImpl.class.getCanonicalName());
private String packageNameSpace = "com.huawei.sdtrp.command";//命令实现类包路径
@Override
public Command parse(String command) {
//这种方法,包名定死了
// String packageNameSpace = "com.huawei.sdtrp.command" ;
String suffix = "Command";
String fullCommandName = packageNameSpace.concat(".").concat(command).concat(suffix);
try {
// return (Command)Class.forName(fullCommandName).newInstance();
return (Command)this.getClass().getClassLoader().loadClass(fullCommandName).newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return null;
/*
String suffix = "Command";
ActionContext ac = ActionContext.getContext();
ac.
WebApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(config.getServletContext());
return (Command)ctx.getBean(command.concat(suffix));
return null;
*/
}
}
//Action
package com.huawei.sdtrp.action;
import com.huawei.sdtrp.command.Command;
import com.huawei.sdtrp.command.CommandParser;
import com.huawei.sdtrp.model.ExecutedResult;
import com.huawei.sdtrp.model.Terminal;
import com.opensymphony.xwork2.ActionSupport;
/**
* 基本Action
* @author cWX194449
*
*/
public abstract class BaseAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private Terminal terminal ;//表单属性搜集
public Terminal getTerminal() {
return terminal;
}
public void setTerminal(Terminal terminal) {
this.terminal = terminal;
}
private CommandParser commandParser ;//注入
public CommandParser getCommandParser() {
return commandParser;
}
public void setCommandParser(CommandParser commandParser) {
this.commandParser = commandParser;
}
private ExecutedResult executedResult ;
public ExecutedResult getExecutedResult() {
return executedResult;
}
public void setExecutedResult(ExecutedResult executedResult) {
this.executedResult = executedResult;
}
@Override
public String execute() throws Exception {
// Object object = this.commandParser.parse(this.terminal.getTerminalData().getCmd()).execute();
// Object object = this.generateCommand(terminal).execute();
// return null == object ?"NULL":object.toString();
if(this.validateParameters()){
this.executedResult = this.generateCommand(terminal).execute();
}
if(null == this.executedResult){//异常,构造命令执行结果
// String msg = this.terminal.getTerminalData().getCmd().concat("命令请求异常");
// throw new Exception(msg);
//TODO 异常命令执行结果,构造
this.executedResult = new ExecutedResult();
}
/*以流的形式,响应终端*/
this.responseTerminal();
// return this.executedResult.getResultStatus();
return null;
}
/**
* 抽象工厂CommandParser产生Command对象
* @param terminal
* @return
*/
public Command generateCommand(Terminal terminal){
// return this.commandParser.parse(this.terminal.getTerminalData().getCmd());
// Command command = this.commandParser.parse(this.terminal.getTerminalData().getCmd());
Command command = this.commandParser.parse(this.terminal.getCmd());
command.init(terminal);
return command ;
};
/**
* 请求参数,检验(default:true相当于不验证)
* 若false,需要重新赋值给命令执行结果
* (过滤非法请求)
* @return
*/
public boolean validateParameters(){
return true;
}
/**
* <pre>
* 以流的形式,写回到终端
* 根据每个每个命令,返回的内容不一样,需要自已定义实现化
* </pre>
*/
public abstract void responseTerminal();
}
//Action
package com.huawei.sdtrp.action;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.huawei.sdtrp.model.ExecutedResult;
import com.opensymphony.xwork2.ActionContext;
/**
* 注册动作
* 1、请求数据校验
* 2、授权检查
* @author cWX194449
*
*/
public class RegisterationAction extends BaseAction {
private static final long serialVersionUID = -4767244117752570601L;
private String encoding = "UTF-8";
@Override
public boolean validateParameters() {
// TODO Auto-generated method stub
return true;
}
@Override
public void responseTerminal() {
ActionContext context = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
Map session = context.getSession();
ExecutedResult executedResult = this.getExecutedResult();
try {
OutputStream out = response.getOutputStream();
byte[] cmdBytes = "Cmd=".concat(executedResult.getCmd()).getBytes(encoding);
out.write(cmdBytes);//命令回写
out.write("&".getBytes(encoding));//分隔符
byte[] codeBytes = "Code=".concat(executedResult.getCode()).getBytes(encoding);
out.write(codeBytes);//状态回写
out.write("&".getBytes(encoding));//分隔符
byte[] descriptionBytes = "Description=".concat(executedResult.getDescription()).getBytes(encoding);
out.write(descriptionBytes);
// out.write("&".getBytes(encoding));//分隔符
out.flush();
// out.close();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
分享到:
相关推荐
在陈家老四的生命即将走到尽头的时候,他用自己对花朵的热爱,为母亲留下了满院的芬芳。这不仅为母亲提供了心灵上的慰藉,也象征着他对生活的热爱和对生命的尊重。他的生命虽短暂,但他所种植的花朵却成为生命延续的...
在这个名为"非常简单的类,留给自己看"的压缩包中,很可能包含了一个或多个简单的C++类定义,供作者个人学习和参考。 一个C++类由以下几个部分组成: 1. **类名**:遵循标识符的规则,通常以大写字母开头,用于...
他用一生的表演和生活经历,让我们明白:真正的快乐,不是没有悲伤和痛苦,而是在这些经历中,我们学会了如何让自己的生活更加有意义,如何用自己的方式去传递爱和欢乐给他人。罗宾·威廉姆斯的故事,是一个永恒的...
参数中-vmargs的意思是设置JVM...非堆就是JVM留给自己用的,所以方法区、JVM内部处理或优化所需的内存(如JIT编译后的代码缓存)、每个类结构(如运行时常数池、字段和方法数据)以及方法和构造方法的代码都在非堆内存中。
司马迁留给明天的是对理想和事业的不懈追求,是对自己信念的坚守。他告诉我们,无论面对多么艰难的环境,都应保持对理想的追求,对事业的忠诚。 女词人李清照,笔下的易安居士,她的词作感情细腻,寄托了对丈夫明诚...
作者将希望比作一朵花,提醒我们要在日常生活中不断浇灌它,用时间、努力和想象去培养它。哪怕我们面前的道路再长再艰难,只要我们不停地努力,最终能够到达成功的彼岸。这种积极向上的人生哲学告诉我们,要有耐心,...
同时,文章也鼓励我们珍惜自己的情感,将感情留给那些真正懂得欣赏和珍惜我们的人。 4. 懂得与爱的关系:文章还表达了懂得一个人意味着爱的深度。当一个人能够理解我们的言行背后的情感和想法,这种理解力就是一种...
此外,遗赠是公民通过遗嘱将自己的部分或全部财产赠予国家、集体或法定继承人之外的个人,这种行为在死亡时生效。案例中,比尔·盖茨将大部分财产留给基金会,体现了遗赠的概念。 对于未成年人的继承权,我国宪法和...
linux虚拟机的搭档,留给自己用的,找着方便
Spring MVC 是一个基于 Java 的轻量级 Web 开发框架,它是 Spring 框架...通过不断地实践和调试,你将逐渐掌握 Spring MVC 的核心概念,并能够搭建起自己的应用程序。这个过程中积累的经验和记录将对你的学习大有裨益。
《财产属于谁留给谁》教学课件以贴近生活的案例为载体,将枯燥的法律条文转化为生动的教学内容,使学生们在轻松愉快的氛围中,掌握了财产所有权与继承权的基本知识,同时也学会了如何在实际生活中运用法律保护自己的...
课程依据课程标准中的“我与他人的关系”部分,特别是关于“权利和义务”的阐述,强调未成年人的财产继承权不容侵犯,鼓励学生学会用法律手段保护自己的合法权益。教学目标包括让学生掌握遗产、被继承人、继承人和...
yodao字典,非常牛的,留给自己用,她具有翻译整篇文章的功能,看英文资料很方便
在中学政治课程中,学习关于“财产留给谁”的教案是一个帮助学生理解和掌握遗产继承法律知识的重要环节。本教案着重于对遗产继承相关法律概念的阐释与实践,旨在帮助八年级学生建立起对遗产继承制度的基本认识。 ...
小学美术课程是培养孩子们艺术素养和创造力的重要环节,六年级下册的主题——“留给母校的纪念”,旨在引导学生们通过艺术创作表达对母校的感激与怀念之情。这份PPT教案为教师提供了一套全面的教学方案,涵盖了多种...
教学案中提供了具体案例,如小明的网易通被损坏,村民老张的承包地被村委会侵犯等,用以说明公民如何依法维护自己的财产权利。小明可以通过与侵权者协商或者提起诉讼的方式寻求法律援助。老张也应当依法保护自己的...
一款常用的android app框架,里面实现了侧边栏,下拉刷新,以及类似QQ底部tab切换的效果,使用这个框架可以搭建自己的app,只需要修改布局文件,里面有很多没实现的地方,提交上来一是留给自己将来完善,二是留给...
通过阅读《印象笔记留给你的空间》这本书,不仅能够学到如何有效使用印象笔记这款工具,更重要的是能够了解到如何构建一个属于自己的个人成长系统。这一系统不仅能够帮助我们在日常生活中更加高效地管理信息,还能够...
在《XXXX年3月财产留给谁22.pptx》这份文档中,主要内容探讨了财产归属、财产所有权保护以及财产继承权的相关知识。 首先,财产分类包括公民个人合法财产,如P68和P69中可能提及的房产、存款、家电等。财产所有权是...