Java 在jsp中使用if语句显示警报';此用户名已被采用';通过struts查看操作页面的结果
我正在尝试显示此if语句Java 在jsp中使用if语句显示警报';此用户名已被采用';通过struts查看操作页面的结果,java,jsp,struts2,Java,Jsp,Struts2,我正在尝试显示此if语句 <s:hidden id="userNameTest" value="nameTest"></s:hidden> <s:if test="userNameTest.value.equals('false')"> That name is already in use. </s:if> 我在jsp onclick中尝试将隐藏字段设置为false,在上面
<s:hidden id="userNameTest" value="nameTest"></s:hidden>
<s:if test="userNameTest.value.equals('false')">
That name is already in use.
</s:if>
我在jsp onclick中尝试将隐藏字段设置为false,在上面的操作中尝试将其设置为false。我在其他网站上查过,但找不到我想要的东西
s:debug
或像
那样打印它用户名已获取
请使用JSTL核心标记。按照惯例,前缀将是
解决方案是在操作类中设置请求属性nameTest
:
布尔比较:
<c:if test="nameTest eq false">
..
</c:if>
<c:if test="nameTest eq 'false'">
..
</c:if>
..
字符串比较:
<c:if test="nameTest eq false">
..
</c:if>
<c:if test="nameTest eq 'false'">
..
</c:if>
..
如果变量nameTest
为布尔值,则取用户名将起作用。尝试以下操作
1)
<s:if test="'${nameTest}'=='false'">
That name is already in use.
</s:if>
1)
该名称已在使用中。
2) 如果该变量是布尔型的,请尝试以下方法
<s:if test="%{${nameTest} == false}">
That name is already in use.
</s:if>
该名称已在使用中。
nameTest
必须在action类中包含getter和setter因为您使用的是struts2,所以基于结果显示警报的更通用方法如下(我在这里也使用Bootstrap框架,因为它提供了更好的外观、感觉和响应能力):
填写表单并提交后,action类完成其工作并返回“成功”或“错误”。然后将其映射到struts.xml中,您应该在其中执行此操作-
<action name="the-form-action" class="com.example.action.MyActionClass">
<result name="success">/result-page.jsp?result=true</result>
<result name="error">/result-page.jsp?result=false</result>
</action>
/result page.jsp?result=true
/result page.jsp?result=false
在result-page.jsp(也可以是您的初始表单页面)中添加以下代码
//Tags and everything
<body>
<%
String result=request.getParameter("result");
if (result==null)
result="";
if (result.equalsIgnoreCase("true"))
{
%>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Success!</strong> Who's the boss?! You're the boss!!
</div>
<%
}
if (result.equalsIgnoreCase("false"))
{
%>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Oops!</strong> There seems to be an error! You may try the following :<br>
<ul>
<li>Fill the compulsory fields</li>
<li>Pray to god</li>
</ul>
</div>
<%
}
%>
//Rest of the body
</body>
//标签和所有东西
&时代;
成功谁是老板?!你是老板!!
&时代;
哎呀似乎有错误!您可以尝试以下操作:
- 填写必填字段
- 向上帝祈祷
//身体的其他部分
现在,警报将仅显示是否成功或失败。当我在加载页面上显示它时,nameTest为false,但if仍然没有显示。我是否尝试以错误的方式执行此操作?代码在if语句旁边工作,我只想让用户知道为什么create不起作用。您是否为nameTestYup创建了getter/setter。我最初是这样做的,但我记得在我开始在线搜索之前。要求是使用struts。我从未使用过JSTL,不过我会在其他项目中研究它。您可以在Struts2项目中使用JSTL和Struts2标记,尽管我不明白为什么需要JSTL,但OGNL更具表现力。