Java 在JSP MVC中加载URL时如何调用命令?
我正在使用找到的设计模式。它有一个FrontController、命令->服务和Dao 如果我在浏览器中加载此URL(viewprofile是jsp的名称):Java 在JSP MVC中加载URL时如何调用命令?,java,model-view-controller,jsp,Java,Model View Controller,Jsp,我正在使用找到的设计模式。它有一个FrontController、命令->服务和Dao 如果我在浏览器中加载此URL(viewprofile是jsp的名称): http://localhost:8080/MyApplication/FrontController/viewprofile 我不知道如何自动调用ViewProfileCommand,然后显示用户的详细信息 目前我的方式是这样的,为了调用ViewProfileCommand,我必须将它连接到URL上,如:http://localhost
http://localhost:8080/MyApplication/FrontController/viewprofile
我不知道如何自动调用ViewProfileCommand
,然后显示用户的详细信息
目前我的方式是这样的,为了调用ViewProfileCommand
,我必须将它连接到URL上,如:http://localhost:8080/MyApplication/FrontController/viewprofile?action=ViewProfile
如何将jsp映射到命令,而不必将其连接到URL?谢谢
FrontController:
package com.secret.bookstore.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.secret.bookstore.command.Command;
import com.secret.bookstore.command.CommandFactory;
import com.secret.bookstore.exceptions.CommandCreationException;
import com.sun.net.httpserver.Filter.Chain;
/*
* Front Controller (Mediator Pattern)
*/
@WebServlet(urlPatterns={"/FrontController"})
public class FrontController extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public FrontController(){
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response){
String action = request.getParameter("action");
CommandFactory commandFactory = CommandFactory.getInstance();
Command command = null;
String view = null;
view = request.getPathInfo().substring(1);
if(action != null){
try {
command = commandFactory.createCommand(action);
view = command.execute(request, response);
} catch(CommandCreationException e) {
e.printStackTrace();
}
}
forwardToPage(request, response, view);
}
/**
* Forward to server to the supplied page
*/
private void forwardToPage(HttpServletRequest request, HttpServletResponse response, String view){
//Get the request dispatcher object and forward the request to the appropriate JSP page...
if(view.equals(request.getPathInfo().substring(1))){
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/" + view + ".jsp");
try {
dispatcher.forward(request, response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try {
response.sendRedirect(view);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
您已将前端控制器设计为基于请求参数而不是请求路径信息来确定操作。因此,您必须提供请求参数才能调用操作 链接答案中的前端控制器示例根据请求方法和请求路径信息确定操作,这也是一种更合理的方法。为了实现能够预处理GET请求的功能需求,您需要执行相同的操作 基本上:
command = commandFactory.createCommand(request.getMethod(), request.getPathInfo());
或者正如给定的示例中所示:
command = commandFactory.createCommand(request);