在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  HTML/ 添加數(shù)據(jù)以后jsp如何獲得添加后的數(shù)據(jù)?

添加數(shù)據(jù)以后jsp如何獲得添加后的數(shù)據(jù)?

Web博客系統(tǒng)(SSM)
有文章Article和文章類別ArticleType兩個(gè)表,不同的Controller和Service。
現(xiàn)在在添加文章類別頁(yè)面添加以后,打開添加文章的頁(yè)面可以看到剛才添加的文章類別(文章類別用 <c:forEach>輸出)
之前使用的方法 是使用ServletContextListener,但是添加后的文章類別無(wú)法顯示。
請(qǐng)問這樣的操作應(yīng)該怎么用監(jiān)聽器實(shí)現(xiàn)?有沒有相應(yīng)的例子?
不甚感激!

回答
編輯回答
北城荒

把內(nèi)容存儲(chǔ)的session中啊.然后在jsp頁(yè)面在從session中取出來(lái)

2017年10月23日 17:40
編輯回答
情殺

這個(gè)我知道的有兩種方法

1. 用Model和EL表達(dá)式

在Controler的參數(shù)中添加Model, 并用addAttribute()方法添加元素, 如:

@RequestMapping(value = {"message-queue-tasks.html"})
public String messageQueueTasks(String messageId, ModelMap model) {
    model.addAttribute("messageId", messageId);
    return "admin/pages/message-queue-tasks";
}

然后在jsp頁(yè)面中直接用EL表達(dá)式輸出, 如:

<blockquote class="layui-elem-quote layui-text">
    ID為${messageId}的消息消費(fèi)記錄記錄
</blockquote>

注意, 在JSP中用EL表達(dá)式一定要jsp文件上面添加:

<%@ page isELIgnored="false"%>

2. 用Request來(lái)傳遞參數(shù)

在Controller的參數(shù)中添加HttpServletRequest request, 并在代碼中用setAttribute()方法添加元素, 如:

@RequestMapping(value = {"stack", "stack.html"})
public String stack(String cacheKey, HttpServletRequest request) {
    try {
        if (!StringUtils.isBlank(cacheKey)) {
            String stackStr = (String) redisUtil.get(cacheKey);
            if (StringUtils.isNotBlank(stackStr)) {
                request.setAttribute("cacheKey", cacheKey);
                request.setAttribute("stackStr", stackStr);
            }
        }
    } catch (Exception e) {
        logger.error("查詢Stack緩存異常", e);
        AlertOver.error("查詢Stack緩存異常", e);
    }
    return "admin/pages/stack";
}

然后在jsp頁(yè)面中用java代碼來(lái)輸出:

<%
    String cacheKey = (String) request.getAttribute("cacheKey");
    String stackStr = (String) request.getAttribute("stackStr");
%>
<h3>
    緩存Key: <%=cacheKey%><br/><br/>
</h3>
2017年6月23日 19:06