Java学习11.01(Javaweb动 态 赋 值)

<c:if>标签的使用

<c:if>标签是标准标签库核心标记库的一个标签,可以根据特定的条件进行输出,相当于 Java 语言中的 if 语句。因为在动态赋值的过程中,需要根据条件控制输出,所以会使用
该标签。该标签的基本语法格式如下:
<c:if test="表达式" [var="name"] [scope="application|session|request|page"]>标签体</c:if>
 
,test 属性表示测试条件,若测试条件为 true,则执行标签体中的内容,否则不执行。
 

基本表单元素赋值

对于基本信息,可直接通过 value 属性赋值。例如在本章的实例中,把用户输入的用户名重新显示在输入框中,使用的是下面的代码:
<input type="text" name="username" value="${param.username}">
 

单选按钮的赋值

单选按钮、复选框和下拉框的使用比较复杂。如果是单选按钮,我们不能直接通过 value
为其赋值,需要一个一个地判断,判断哪个选项的值与用户选择的值相同,如果相同,这
个选项就是用户选择的值。下面是修改用户学历信息时使用的代码:
 
<input type="radio" name="degree" value="专科"
<c:if test="${param.degree == "专科"}">checked</c:if>>专科
<input type="radio" name="degree" value="本科"
<c:if test="${param.degree == "本科"}">checked</c:if>>本科
<input type="radio" name="degree" value="硕士研究生"
<c:if test="${param.degree == "硕士"}">checked</c:if>>硕士
<input type="radio" name="degree" value="博士研究生"
<c:if test="${param.degree == "博士"}">checked</c:if>>博士
<input type="radio" name="degree" value="其他"
<c:if test="${param.degree == "其他"}">checked</c:if>>其他
 
复选框的使用与单选按钮的使用基本相同。

下拉框的赋值

下拉框赋值的具体用法如下:
<select name="local">
<option value="华东">华东</option>
<option value="华南"
<c:if test="${param.local == "华南"}">selected</c:if> >华南</option>
<option value="华北"
<c:if test="${param.local == "华北"}">selected</c:if> >华北</option>
<option value="东南"
<c:if test="${param.local == "东南"}">selected</c:if> >东南</option>
<option value="西南"
<c:if test="${param.local == "西南"}">selected</c:if> >西南</option>
<option value="西北"
<c:if test="${param.local == "西北"}">selected</c:if> >西北</option>
<option value="东北"
<c:if test="${param.local == "东北"}">selected</c:if> >东北</option>
<option value="华中"
<c:if test="${param.local == "华中"}">selected</c:if> >华中</option>
</select>
 

多行文本框的赋值

多行文本框也不能使用 value 赋值,需要把值放在开始标志和结束标志之间。例如:
<textarea rows="8" name="comment" cols="40">
${param.comment}
</textarea>
原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/14204409.html