简体   繁体   English

使用Struts2无法从动作跳至Jsp

[英]Cannot skip from an action to a Jsp with Struts2

I make 2 actions, action1 is aimed to update some info, action2 is to retrieve it. 我进行了2次操作,action1旨在更新一些信息,action2则是对其进行检索。 Often, I use action2 to search and a JSP to display. 通常,我使用action2进行搜索,并使用JSP进行显示。 Now there is something more, after seeing the JSP, firstly enter action1 to modify something, then goto action2 to retrieve, finally skip to the Jsp to display the info. 现在有了更多东西,看到JSP之后,首先输入action1进行修改,然后转到action2进行检索,最后跳到Jsp以显示信息。 I use Struts2 to achieve it. 我使用Struts2来实现。 But in fact the info is not changed, I need to refresh the webpage so that it can be displayed normally. 但是实际上信息并没有改变,我需要刷新网页以便可以正常显示。 At first I guess it's cache who leads to this condition. 起初我想是导致这种情况的原因是缓存。 So I add some tags in JSP to avoid this and rewrite the url using ?time=${time}, but still in vain. 因此,我在JSP中添加了一些标签来避免这种情况,并使用?time = $ {time}重写了url,但仍然徒劳。 My code is as follows. 我的代码如下。

MessageAction.java: MessageAction.java:

    public String getMessage() throws Exception{
        Map<String, Object> session = ActionContext.getContext().getSession();      
        Account account = (Account)(session.get("account"));
        List<Message> messages = messageBo.getMessageByAccount(account);
        session.remove("messages");
        session.put("messages", messages);
        time = Long.toHexString(new Date().getTime());
        System.out.println(time);
        return SUCCESS;

    }

    @Action(value="modifyMessage",
            results={
            @Result(name="success",location="/getMessage.action",type="redirectAction")})
    public String modifyMessage() throws Exception{ 
        HttpServletRequest request = ServletActionContext.getRequest();             
        String field = request.getParameter("field");
        String value = request.getParameter("value");
        Integer id = Integer.parseInt(request.getParameter("id"));      
        messageBo.updateMessageByField(id, field, value);
        return SUCCESS;
    }

struts.xml: struts.xml:

    <action name="getMessage" class="com.ailonger.action.MessageAction" method="getMessage">  
        <result type="redirect">/room/my_message.jsp?time=${time}</result>  
    </action>

How to skip to JSP directly after entering action2? 进入action2后如何直接跳到JSP?

You should never redirect to JSP from the action. 您永远不要从该操作重定向到JSP。 The code below is redirect result type that loose any request attributes and valueStack from the previous action, so the jsp view becomes without model. 下面的代码是redirect结果类型,该结果类型从上一个操作中valueStack了所有请求属性和valueStack ,因此jsp视图变得没有模型。 The new action context is created for the new request which is result of redirection to the jsp. 为新请求创建了新的动作上下文,这是重定向到jsp的结果。

<action name="getMessage" class="com.ailonger.action.MessageAction" method="getMessage">  
    <result type="redirect">/room/my_message.jsp?time=${time}</result>  
</action>

The code should change to use dispatcher result type that is default and could be omitted in xml configuration. 该代码应更改为使用默认的dispatcher结果类型,并且可以在xml配置中省略。

<action name="getMessage" class="com.ailonger.action.MessageAction" method="getMessage">  
    <result>/room/my_message.jsp?time=${time}</result>  
</action>

不确定您的要求。如果尝试将请求从servlet转发到另一个资源(servlet,JSP文件),请使用

forward(ServletRequest request, ServletResponse response)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM