POST-REDIRECT-GET消失参数值

时间:2015-01-28 00:04:25

标签: java servlets post-redirect-get

我正在实现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。

1 个答案:

答案 0 :(得分:0)

正如@xehpuk指出我必须编码非ASCII字符,这就是我遇到这个问题的原因。所以在return语句中我应该做以下事情:

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