我正在实现Front Controller,它是一个servlet和Command模式设计。我遇到的问题:POST成功后我会重定向用户以查看附加到请求的一个参数。并且在通过控制器处理请求之后,我有参数的名称,但不是值。
例如,如果controller
应该继续,那么下一条路径将转到Command:
controller?command=viewFaculty&name=Автоматика и приборостроения
结果路径如下:
controller?command=viewFaculty&name=
我的控制器:
public class FrontController extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(FrontController.class);
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
process(request, response, ActionType.FORWARD);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
process(request, response, ActionType.REDIRECT);
}
private void process(HttpServletRequest request,
HttpServletResponse response, ActionType actionType)
throws IOException, ServletException {
LOG.debug("Start processing in Controller");
// 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 = CommandManager.get(commandName);
LOG.trace("Obtained 'command' = " + command);
Map<String, String[]> parameterMap = request.getParameterMap();
for (Iterator<Entry<String, String[]>> iterator = parameterMap
.entrySet().iterator(); iterator.hasNext();) {
Entry<String, String[]> entry = iterator.next();
System.out.println(entry.getKey() + "=" + Arrays.toString(entry.getValue()));
}
// execute command and get forward address
String path = command.execute(request, response, actionType);
if (path == null) {
LOG.trace("Redirect to address = " + path);
LOG.debug("Controller proccessing finished");
response.sendRedirect(Path.WELCOME_PAGE);
} else {
if (actionType == ActionType.FORWARD) {
LOG.trace("Forward to address = " + path);
LOG.debug("Controller proccessing finished");
RequestDispatcher disp = request.getRequestDispatcher(path);
disp.forward(request, response);
} else if (actionType == ActionType.REDIRECT) {
LOG.trace("Redirect to address = " + path);
LOG.debug("Controller proccessing finished");
response.sendRedirect(path);
}
}
}
}
parameterMap
次迭代的输出:
command=[viewFaculty]
name=[]
我已经查看了同样的问题并且回答他们正在使用response.encodeRedirectUrl(String)
- 这没有帮助,这个方法也被弃用了。我使用的是Tomcat 7,servlets,jsp,log4j和mysql。
答案 0 :(得分:0)
正如@xehpuk指出我必须编码非ASCII字符,这就是我遇到这个问题的原因。所以在return语句中我应该做以下事情:
return Path.REDIRECT_TO_FACULTY + URLEncoder.encode(facultyName, "UTF-8");