七、javascipt里面的el表达式

2018-07-24  本文已影响0人  lifeline张

newDetailList.jsp页面:

<%@page import="cn.kgc.entity.News"%>
<%@page import="java.util.List"%>
<%@page import="cn.kgc.Service.Impl.NewsDetailServiceImpl"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<<jsp:useBean id="newsService" class="cn.kgc.Service.Impl.NewsDetailServiceImpl"></jsp:useBean>
<%--动态包含无法使用,页面报错,newsService无法使用 <jsp:include page="../common/common.jsp" /> --%>
<%-- <%@include file="../common/common.jsp" %> --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>无标题文档</title>
<link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/common.css"/>
<style type="text/css">
<!--

-->
</style>
<script>
    function addNews(){
        window.location="newsDetailCreateSimple.jsp";
    }
    
    var flag = '${requestScope.flag }';
    alert(flag);
    if (flag != null && flag !="" && flag != undefined) {
        if (flag == "success") {
            alert(flag);
        }
    }
</script>
</head>

<body>

<!--主体-->
    <div class="main-content-right">
        <!--即时新闻-->
        <div class="main-text-box">
            <div class="main-text-box-tbg">
                <div class="main-text-box-bbg">
                    <form name ="searchForm" id="searchForm" action="/news/jsp/admin/newsDetailList.jsp" method="post">
                      <div>
                        新闻分类:
                            <select name="categoryId">
                                <option value="0">全部</option>
                                
                                    <option value='1' >国内</option>
                                
                                    <option value='2' >国际</option>
                                
                                    <option value='3' >娱乐</option>
                                
                                    <option value='4' >军事</option>
                                
                                    <option value='5' >财经</option>
                                
                                    <option value='6' >天气</option>
                                
                            </select>
                        新闻标题<input type="text" name="title" id="title" value=''/>
                            <button type="submit" class="page-btn">GO</button>
                            <button type="button" onclick="addNews();" class="page-btn">增加</button>
                            <input type="hidden" name="currentPageNo" value="1"/>
                            <input type="hidden" name="pageSize" value="10"/>
                            <input type="hidden" name="totalPageCount" value="2"/>
            </div>
            </form>
            <table cellpadding="1" cellspacing="1" class="admin-list">
                <thead >
                    <tr class="admin-list-head">
                        <th>新闻标题</th>
                        <th>作者</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <%
                        List<News> newsList = newsService.getAllNews();
                        int i= 0;
                        for(News news:newsList){
                            i++;
                     %>
                    <tr <%if(i%2==0){ %>class="admin-list-td-h2"<%} %>>
                        <td><a href='newsDetailView.jsp?id=<%=news.getId()%>'><%=news.getTitle() %></a></td>
                        <td><%=news.getAuthor() %></td>
                        <td><%=news.getCreateDate() %></td>
                        <td><a href='adminNewsCreate.jsp?id=2'>修改</a>
                            <a href="javascript:if(confirm('确认是否删除此新闻?')) location='adminNewsDel.jsp?id=2'">删除</a>
                        </td>
                    </tr> 
                    <%} %>
                </tbody>
            </table>
           <div class="page-bar">
            <ul class="page-num-ul clearfix">
                <li>共7条记录&nbsp;&nbsp; 1/2页</li>
                <a href="javascript:page_nav(document.forms[0],2);">下一页</a>
                <a href="javascript:page_nav(document.forms[0],2);">最后一页</a>&nbsp;&nbsp;
            </ul>
         <span class="page-go-form"><label>跳转至</label>
         <input type="text" name="inputPage" id="inputPage" class="page-key" />页
         <button type="button" class="page-btn" onClick='jump_to(document.forms[0],document.getElementById("inputPage").value)'>GO</button>
        </span>
        </div> 
        </div>
       </div>
   </div>
   </div>
<%
        News news = (News)request.getAttribute("news");
        if (news != null) {
            out.print(news.getTitle());
        }
    %>
</body></html>

doAdd.jsp页面:

<%@page import="cn.kgc.Service.Impl.NewsDetailServiceImpl"%>
<%@page import="cn.kgc.entity.News"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<<jsp:useBean id="ndsi" class="cn.kgc.Service.Impl.NewsDetailServiceImpl"></jsp:useBean>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
    News news = new News();
    news.setCategoryId(Integer.parseInt(request.getParameter("categoryId")));
    out.print(request.getParameter("categoryId"));
    news.setTitle(request.getParameter("title"));
    news.setAuthor(request.getParameter("author"));
    news.setSummary(request.getParameter("summary"));
    news.setContent(request.getParameter("newscontent"));
    news.setCreateDate(new Date());
    
    boolean flag = ndsi.addNews(news);
    if (flag) {
        request.setAttribute("flag", 1);
        request.setAttribute("news", news);
        request.getRequestDispatcher("newsDetailList.jsp").forward(request, response);
     // response.sendRedirect(request.getContextPath()+"/jsp/newsDetailList.jsp");
    } else {
        out.print("添加失败,请重新添加");
        response.sendRedirect(request.getContextPath()+"/jsp/newsDetailCreateSimple.jsp");
    }
 %>
</body>
</html>

newsDetailCreateSimple.jsp页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
    <head>
        <link href="<%=request.getContextPath() %>/css/common.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="<%=request.getContextPath() %>/ckeditor/ckeditor.js"> </script>
    </head>
<body>
<form name ="dataFrm" id="dataFrm" action="doAdd.jsp" method="get"  enctype="multipart/form-data">
    <table  width="100%" border="0" cellspacing="5" cellpadding="0">
        <thead>
            <tr><td align="center" colspan="2" class="text_tabledetail2">增加新闻</td></tr>
        </thead>
        <tbody>
            <tr>
                <td style="text-align:right;" class="text_tabledetail2">分类</td>
                <td style="text-align:left;"> 
                <!-- 列出所有的新闻分类 -->
                    <select name="categoryId">
                        <option value="1">国内</option>
                        <option value="2">国际</option>
                        <option value="3">娱乐</option>
                        <option value="4">军事</option>
                        <option value="5">财经</option>
                        <option value="6">天气</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td style="text-align:right;" class="text_tabledetail2">标题</td>
                <td style="text-align:left;"><input type="text" name="title" value=""/></td>
            </tr>
            <tr>
                <td style="text-align:right;" class="text_tabledetail2">作者</td>
                <td style="text-align:left;"><input type="text" name="author" value=""/></td>
            </tr>
            
            <tr>
                <td style="text-align:right;" class="text_tabledetail2">摘要</td>
                <td style="text-align:left;"><textarea id="summary" name="summary" rows="8" cols="50"></textarea></td>
            </tr>
            <tr>
                <td style="text-align:right;" class="text_tabledetail2">内容</td>
                <td style="text-align:left;">
                <div id="xToolbar"></div>
                <textarea id="newscontent" name="newscontent" rows="8" cols="30" class="ckeditor"></textarea></td>
            </tr>
            <tr>
                <td style="text-align:right;" class="text_tabledetail2">上传图片 </td>
                <td style="text-align:left;"><input type="file" name="picPath" value=""/></td>
            </tr>
            <tr>
                <td style="text-align:center;" colspan="2">
                    <button type="submit" class="page-btn" name="save">保存</button>
                    <button type="button" class="page-btn" name="return" onclick="javascript:location.href='newsDetailList.jsp'">返回</button>
                </td>
            </tr>
        </tbody>
    </table>
</form>
</body>
</html>

遇到的bug是:
在doAdd页面调用数据库里面的增加方法之后,给request设置一个值flag,使用request的转发到newDetailList.jsp页面,在newDetailList.jsp页面使用el表达式获取flag的值。
这时候要注意:
第一:在script标签中,如果要使用el表达式,则一定要加引号,否则整个script标签中的函数都无法使用。原因是在script脚本中,定义字符串变量是需要加引号的。
第二:el表达式的各种取值方法的作用域。


image.png

补充:request.setAttribute()里面也可以放对象。

上一篇下一篇

猜你喜欢

热点阅读