【发布时间】:2015-01-18 15:01:26
【问题描述】:
我正在使用 tomcat、jsp、servlet 和 log4j 开发我的第一个 Web 项目,并且我有一个使用命令设计模式的演示,我对此很感兴趣。我有一个控制器,它接受 doGet 和 doPost 方法,然后处理对 CommandContainer 的请求,CommandContainer 找到合适的命令,执行它,获取资源路径并将客户端转发给它。
public abstract class Command implements Serializable {
private static final long serialVersionUID = 8879403039606311780L;
public abstract String execute(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException;
}
管理命令的CommandContainer:
public class CommandContainer {
private static final Logger LOG = Logger.getLogger(CommandContainer.class);
private static Map<String, Command> commands = new TreeMap<String, Command>();
static {
// common commands
commands.put("login", new LoginCommand());
commands.put("logout", new LogoutCommand());
commands.put("viewSettings", new ViewSettingsCommand());
commands.put("noCommand", new NoCommand());
// client commands
commands.put("listMenu", new ListMenuCommand());
// admin commands
commands.put("listOrders", new ListOrdersCommand());
LOG.debug("Command container was successfully initialized");
LOG.trace("Number of commands --> " + commands.size());
}
public static Command get(String commandName) {
if (commandName == null || !commands.containsKey(commandName)) {
LOG.trace("Command not found, name --> " + commandName);
return commands.get("noCommand");
}
return commands.get(commandName);
}
我拥有的唯一控制器:
public class Controller extends HttpServlet {
private static final long serialVersionUID = 2423353715955164816L;
private static final Logger LOG = Logger.getLogger(Controller.class);
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
private void process(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
LOG.debug("Controller starts");
// extract command name from the request
String commandName = request.getParameter("command");
LOG.trace("Request parameter: command --> " + commandName);
// obtain command object by its name
Command command = CommandContainer.get(commandName);
LOG.trace("Obtained command --> " + command);
// execute command and get forward address
String forward = command.execute(request, response);
LOG.trace("Forward address --> " + forward);
LOG.debug("Controller finished, now go to forward address --> " + forward);
// if the forward address is not null go to the address
if (forward != null) {
RequestDispatcher disp = request.getRequestDispatcher(forward);
disp.forward(request, response);
}
}
}
我在jsp中使用Controller的方式如下:
...
<form id="login_form" action="controller" method="post">
<input type="hidden" name="command" value="login"/>
...
</form>
还有web.xml文件:
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>com.mycompany.web.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/controller</url-pattern>
</servlet-mapping>
我不明白如何使用命令模式实现 Post-Redirect-Get 模式,因为每次请求到达控制器时,它都会使用process() 方法,并且似乎在 JSP 中使用 GET 或 POST 并不重要。然后你会帮助理解使用命令模式的需要吗?如果我将使用多个 servlet,例如 LoginServlet、LogoutServlet、ViewSettingsServlet 而不是一个控制器,那会是一个坏主意,因为我需要将它们硬编码为 jsp 表单作为操作?所有这些问题都让我感到困惑,因为我是初学者,所以请让我理解这一切。
【问题讨论】:
标签: java jsp servlets command-pattern post-redirect-get