简体   繁体   中英

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. Often, I use action2 to search and a JSP to display. 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. I use Struts2 to achieve it. 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. My code is as follows.

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:

    <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?

You should never redirect to JSP from the action. 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. The new action context is created for the new request which is result of redirection to the 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.

<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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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