我有一个获取用户信息并将其保存到数据库中的表单。我的表单由文本字段和单选按钮组成。 (我使用的是jsp,bean servlet,jdbc)
现在我已经制作了另一种形式,即编辑用户输入的信息,它还在文本字段中显示他们先前输入的信息。
我可以在文本字段中显示以前输入的信息但我的问题是我无法显示以前选中的单选按钮,我该怎么做?
以下是显示先前输入值
的文本字段示例<tr>
<td>Email:</td>
<td><input type="text" name="editregisterEmail" maxlength="10" size="15" value="<jsp:getProperty name="StudentBean" property="email"/>"></td>
</tr>
问题在于,如何向用户显示之前选中的单选按钮以便他们重新检查?
我有以下代码获取用户检查的上一个单选按钮值
<jsp:getProperty name="StudentBean" property="department"/>
以下是问题
<tr>
<td>Department: </td>
<td>
<input type="radio" name="editregisterDepartment" value="Information Technology"> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration"> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering"> Engineering
</td>
</tr>
答案 0 :(得分:3)
你最好使用表达式语言(EL),如下所示:
<tr>
<td>Department: </td>
<td>
<input type="radio" name="editregisterDepartment" value="Information Technology" ${requestScope['StudentBean'].department == 'Information Technology'? 'checked':''}> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration" ${requestScope['StudentBean'].department == 'Business Administration'? 'checked':''}> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering" ${requestScope['StudentBean'].department == 'Engineering'? 'checked':''}> Engineering
</td>
</tr>
requestScope可以根据您定义bean的范围进行更改
答案 1 :(得分:0)
一个简单的开关就可以了。
<%
StudentBean s=new StudentBean();
switch(s.getDepartment()){
case "Information Technology":
%>
<input type="radio" name="editregisterDepartment" value="Information Technology" checked> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration"> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering"> Engineering
<%
break;
case "Business Administration":
%>
<input type="radio" name="editregisterDepartment" value="Information Technology"> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration" checked> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering"> Engineering
<%
break;
case "Engineering":
%>
<input type="radio" name="editregisterDepartment" value="Information Technology"> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration"> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering" checked> Engineering
<%
break;
}
%>
如果你没有使用JavaEE 7,你将无法打开字符串值,那么你将不得不使用if
,如下所示:
<%
StudentBean s=new StudentBean();
if(s.getDepartment().equals("Information Technology"){
%>
<input type="radio" name="editregisterDepartment" value="Information Technology" checked> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration"> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering"> Engineering
<%
}else if(s.getDepartment().equals("Business Administration"){
%>
<input type="radio" name="editregisterDepartment" value="Information Technology"> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration" checked> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering"> Engineering
<%
}else if(s.getDepartment().equals("Engineering"){
%>
<input type="radio" name="editregisterDepartment" value="Information Technology"> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration"> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering" checked> Engineering
<%
}
%>
答案 2 :(得分:0)
我使用了if条件。它在我的jsp页面中对我有用。我正在使用java 8和apache tomcat 6。
<%
String id=request.getParameter("id");
User u=UserDao.getRecordById(Integer.parseInt(id));
%>
...
...
<tr><td>Sex:</td>
<td>
<%if(u.getSex().equalsIgnoreCase("male")) { %>
<input type="radio" name="sex" value="male" checked/>Male
<input type="radio" name="sex" value="female"/>Female
<% } else if(u.getSex().equalsIgnoreCase("female")) {%>
<input type="radio" name="sex" value="male" />Male
<input type="radio" name="sex" value="female" checked/>Female
<% } %>
</td></tr>