POST-REDIRECT-GET 消失的参数值

POST-REDIRECT-GET vanishing parameter values

我正在实施前端控制器,这是一个 servlet 和命令模式设计。我遇到的问题:成功 POST 后,我正在重定向用户以查看附加到请求的一个参数。在通过控制器处理请求后,我得到了参数的名称,但没有值。

例如,如果 controller 应该继续执行命令返回的下一条路径:

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、servlet、jsp、log4j 和 mysql.

正如@xehpuk 指出的那样,我必须对非 ASCII 字符进行编码,这就是我遇到该问题的原因。所以在 return 声明中我应该做以下事情:

return Path.REDIRECT_TO_FACULTY + URLEncoder.encode(facultyName, "UTF-8");