简体   繁体   English

Java EE在scriptlet中没有文本字段吗?

[英]Does Java EE not have text fields in scriptlets?

So I have been wandering around the web for some time looking for this answer. 因此,我一直在网上徘徊一段时间以寻找答案。 Is there no way of having text fields in java scriptlets for a JSP? 是否没有办法在JSP的Java脚本中包含文本字段? I know that in C# you can have 我知道在C#中您可以拥有

@Html.TextField();

And that will create a text field for you to use as C# code. 这将创建一个文本字段供您用作C#代码。

Why I would like this, is that I am grabbing a session and checking to see if the user attribute has a user name stored. 我之所以这么想,是因为我正在抓取一个会话并检查user属性是否存储了用户名。 If it does I don't want it to display the username textbox. 如果不是,我不希望它显示用户名文本框。 Although from what I can tell I can't do that with java scriptlets. 虽然从我可以告诉我的角度来看,我无法使用Java scriptlet做到这一点。

<%if(session.getAttribute("user") == null) %>
Username: <input type="text" name="name"><br/>

Is there something that I am missing? 有什么我想念的吗?

This will work, but it's better to use JavaServer Pages Standard Tag Library and Expression language 这将起作用,但是最好使用JavaServer Pages标准标记库表达式语言
instead of scriptlets. 而不是脚本。 It tend to complicate maintenance for JSP-pages. 这会使JSP页面的维护复杂化。

You can set a scoped variable on the JSP page. 您可以在JSP页面上设置范围变量。 In this manner: 以这种方式:

<c:set var="myVariable" scope="page" value="expression"/>

Your situation can be easily solved with the help of JSTL with EL: 借助带有EL的JSTL,可以轻松解决您的情况:

<c:if test="${sessionScope.user eq null}">
   <c:out value="Username: ${myVariable}" />
</c:if>

If you have a more complex logic then it makes sense to create a custom tags . 如果您有更复杂的逻辑,那么创建自定义标签是有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM