从项目中学习jsp知识

1. 如何获取路径(jsp在项目中最最频繁的应用)

一般的项目通常都会存在包中包的现象,在jsp页面填写路径的时候不免有些恼火,那么我们可以通过动态获取路径,如下:

.<form class="navbar-form pull-right" action="${pageContext.request.contextPath}/LoginAction" method="post"> ---------->获取动态路径,跳转到action

================================================================================================================

2.如何从jsp页面向action中传值

<a href="${pageContext.request.contextPath }/student/QueryOneIneractAction?id=${i.interactId}" style="text-decoration: none;" >查看详情</a>.

划线处是通过el表达式实现的,要想让这行代码行得通,前提是你要在某个action中存入数据在作用域中(request,session等)

例如,你已将集合中的数据存入request作用域中,那么在jsp页面中可以通过<c:forEach items="${list}" var="i"> ### </c:forEach>

通过变量 i 来遍历出list中的数据 既:${i.interactId}

================================================================================================================

3.在jsp页面中实现判断,类似java中的 if 功能:<c:if test =  "${判断条件}">要执行的功能,通常是一个超链接</c:if>  例如

例如:如果未回复,则可删除.   如果回复了,则不可以删除

<c:if test=${i.flagAnswer == 'n'}>   ------------->n是未回复--------->此处要想些的漂亮点可以加一个onclick事件

<a href = "执行删除的action功能的路径">

</c:if>

<c:if test=${i.flagAnswer == 'y'}>   ------------->y是回复

<a href = "##"> ------->此处不填写路径

</c:if>

================================================================================================================

4.   如何在超链接中添加onclick事件(以3.的代码为例)--------<c:if>判断在下面讲

<c:if test=${i.flagAnswer == 'n'}>   ------------->y是回复

<a href = "##" onclick = return judgeFunction();>-------------------------->做一个弹框功能

</c:if>

-----------------------------------------------------------------------------------------------------------------------------------------------------

<head>

<script>

funtion judgeFuntion()

{

var v =  windoe.confirm("您确定要删除么?");

if(v == true)

{

return true;

}else{

return false;

}

}

</script>

</head>

================================================================================================================

5. 通过<forEach>循环,嵌套-------得到实体类中的“属性的属性”

首先在User的实体类中有一个-----------------private List<Course> course = new ArrayList<Course>();的属性

Course类中有courseName的属性。我们通过jsp页面的<c:forEach>遍历出想要的结果

例如:(1)通过request作用域(最好不用,优缺点------>可以看四大作用域了解)

action:
List<User> list = is.queryAllTeacher();
request.setAttribute("list", list);
  ---------------------------------->在action中将存有User集合存入request作用域中

jsp页面:

通过嵌套<c:forEach>拿到得到实体类中的“属性的属性”注意代码操作

<c:forEach items="${list}" var="i">
        <tr height="10px;" >
        <tr>
       
       
            <th style="text-align: center;">${i.username}</th>
<th style="text-align: center;">${i.realname}</th>
<th style="text-align: center;">${i.department}</th>
<th style="text-align: center;">
<c:forEach items="${i.course }" var="m" varStatus = "b">
<c:if test="${b.last }">${m.courseName }</c:if>
<c:if test = "${not b.last }">${m.courseName },</c:if>
</c:forEach>
</th>
<th style="text-align: center;"><a href="${pageContext.request.contextPath }/student/createInteract.jsp?id=${i.userId}"
style="text-decoration: none;">发起提问</a> &nbsp;&nbsp;&nbsp;</th>
           &nbsp;&nbsp;&nbsp;
           
           </th>
          </tr>
          </c:forEach
>


(2)

(1)通过Sessiont作用域

action:
List<User> list = is.queryAllTeacher();
H
ttpSession session = request.getSession(true);

session.setAttribute("list",list);---------------------------->存入Session作用域

jsp页面:

通过嵌套<c:forEach>拿到得到实体类中的“属性的属性”注意代码操作

<c:forEach items="${sessionScope.list}" var="i">
        <tr height="10px;" >
        <tr>
       
       
            <th style="text-align: center;">${i.username}</th>
<th style="text-align: center;">${i.realname}</th>
<th style="text-align: center;">${i.department}</th>
<th style="text-align: center;">
<c:forEach items="${i.course }" var="m" varStatus = "b">
<c:if test="${b.last }">${m.courseName }</c:if>
<c:if test = "${not b.last }">${m.courseName },</c:if>
</c:forEach>
</th>
<th style="text-align: center;"><a href="${pageContext.request.contextPath }/student/createInteract.jsp?id=${i.userId}"
style="text-decoration: none;">发起提问</a> &nbsp;&nbsp;&nbsp;</th>
           &nbsp;&nbsp;&nbsp;
           
           </th>
          </tr>
          </c:forEach
>









     

================================================================================================================

6. 通过el表达式获取客户端参数

${param.属性}  通过el表达式里面的关键字param获取   ----------------很常见的!!

例如在页面上获取action的异常信息
action:
try{
#########
}catch(Exception e){
String message = e.getMessage();
String mess = java.net.URLEncoder.encode(message, "utf-8");
response.sendRedirect(request.getContextPath()+"/LoginView.jsp?mess="+mess);
}
jsp页面通过:${param.mess }就可以回显以后吃那个信息


================================================================================================================


7.

在jsp页面进行判断。<c:if>
//将页面中的字母变成汉字

<c:if test="${i.flagAnswer=='n'}">
         
         </c:if>
         
         <c:if test="${i.flagAnswer=='y'}">
         
          </c:if>


=============================================================2015.5.21===================================================






















评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值