怎么将<c:if test="${fn:length(userInfo.phone)>10}"> ${fn:substring(userInfo.phone, 0, 3)}****${fn:substring(userInfo.phone, 7, 11)} </c:if>放入<input>标签中的
时间: 2024-04-10 15:33:40 浏览: 19
你可以使用 JSP 的标签库将<c:if>标签中的内容放入<input>标签中。具体操作如下:
```jsp
<input type="text" name="phone" value="<c:if test="${fn:length(userInfo.phone)>10}">
${fn:substring(userInfo.phone, 0, 3)}****${fn:substring(userInfo.phone, 7, 11)}
</c:if>" />
```
这样,如果userInfo.phone的长度大于10,则会显示带有隐藏部分的手机号码;否则,会显示完整的手机号码。
相关问题
你可以使用 JSP 的标签库将<c:if>标签中的内容放入<input>标签中。具体操作如下: <input type="text" name="phone" value="<c:if test="${fn:length(userInfo.phone)>10}"> ${fn:substring(userInfo.phone, 0, 3)}****${fn:substring(userInfo.phone, 7, 11)} </c:if>" /> 这样,如果userInfo.phone的长度大于10,则会显示带有隐藏部分的手机号码;否则,会显示完整的手机号码。不回显
抱歉,我之前的回答有误。在JSP中无法直接将<c:if>标签中的内容放入<input>标签的value属性中。如果你想要根据条件来显示不同的值,可以使用JSTL的<c:set>标签来设置一个新的变量,然后将这个变量放入<input>标签的value属性中。具体操作如下:
```jsp
<c:set var="phoneValue">
<c:if test="${fn:length(userInfo.phone) > 10}">
${fn:substring(userInfo.phone, 0, 3)}****${fn:substring(userInfo.phone, 7, 11)}
</c:if>
</c:set>
<input type="text" name="phone" value="${phoneValue}" />
```
这样,如果userInfo.phone的长度大于10,则会将隐藏部分的手机号码赋值给phoneValue变量;否则,将完整的手机号码赋值给phoneValue变量。最后,将phoneValue变量放入<input>标签的value属性中。这样做可以实现根据条件显示不同值,并将其赋值给<input>标签。
将<c:choose> <c:when test="${fn:length(user.name)>2}"> ${fn:substring(user.name, 0, 1)}*${fn:substring(user.name, fn:length(user.name)-1, fn:length(user.name))} </c:when> <c:when test="${fn:length(user.name)<=2 && fn:length(user.name)>0}"> ${fn:substring(user.name, 0, 1)}* </c:when> <c:otherwise> </c:otherwise> </c:choose>写人String verifyName = verifyUser.getPhone(); if (verifyName.length() > 2){ } String idName = userInfo.getName(); // 进行比较 if (idName.equals(verifyName)) { userInfo.setName(verifyUser.getName()); } if (userInfo.getName().contains("*")){ out.print("<script>top.Dialog.alert('名字异常无法保存');window.location.href='id?u=" + userInfo.getUsername() + "';</script>"); out.close(); return; }
在给定的代码片段中,将`<c:choose>`、`<c:when>` 和 `<c:otherwise>` 标签替换为相应的代码逻辑,并将处理用户姓名的部分插入到Controller中:
```java
String verifyName = verifyUser.getPhone();
if (verifyName.length() > 2) {
verifyName = verifyName.substring(0, 1) + "*" + verifyName.substring(verifyName.length() - 1);
}
String idName = userInfo.getName();
// 进行比较
if (idName.equals(verifyName)) {
userInfo.setName(verifyUser.getName());
}
if (userInfo.getName().contains("*")) {
out.print("<script>top.Dialog.alert('名字异常无法保存');window.location.href='id?u=" + userInfo.getUsername() + "';</script>");
out.close();
return;
}
```
在上述代码中,我将原先的`<c:choose>`、`<c:when>` 和 `<c:otherwise>` 标签替换为Java代码逻辑,并将用户姓名处理的部分放置在验证手机号码后。现在你可以将这段代码插入到你的Controller中以实现相应的功能。