使用struts2在jsp页面中显示字符串的值
我正在尝试在 JSP 页面中使用 Struts2 显示字符串的值。
<%String name="Sumit"; %>
Name: <s:property value="name"/>
但它不显示任何内容。
I'm trying to display value of string using Struts2 in JSP page.
<%String name="Sumit"; %>
Name: <s:property value="name"/>
But it doesn't display anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Scriptlet 和 taglib 不共享相同的变量范围。使用其中之一,而不是同时使用两者。
Scriptlets and taglibs doesn't share the same variable scope. Use the one or the other, not both.
一些评论/建议:
1)为了详细说明 BalusC 所说的内容,您编写的 scriptlet(即 <% String name="submit"; %> 与对操作属性的引用(即,)是分开的
2) 通常,人们所做的是编写一个操作类并将其与 struts2.xml 中的特定 URL 相关联。假设您执行此操作并编写一个名为 MyAction 的操作。当您在 JSP 中调用引用时,实际上是在“MyAction”类的实例上调用“getName()”。想必,您将遵循操作类的命令模式,并在操作类的execute() 方法中构造一个有用的名称值。
3) 定义本地 Java 值的 Scriptlet 在 JSP 处理期间确实很有用(我发现每当我与非 Struts2 框架(如 DisplayTagg)集成时它们都很有帮助),但它们与操作无关。您只需将一些临时 Java 变量设置为一个值即可。当您使用它时,您需要以 scriptlet 样式取消引用它(例如,<%= name %> 等)
A few comments/suggestions:
1) To elaborate a bit on what BalusC said, the scriptlet you wrote (i.e., <% String name="submit"; %> is separate from the reference to an action property (i.e., )
2) Typically, what people do is they write an action class and associate it with a particular URL in their struts2.xml. So let's say you do this and write an action called MyAction. When you call reference in a JSP, you're really calling "getName()" on an instance of the "MyAction" class. Presumably, you will follow the command pattern of action classes and construct a useful value for name in the execute() method of your Action class.
3) Scriptlets that define local Java values can indeed be useful during JSP processing (I find they are helpful whenever I'm integrating with non Struts2 frameworks like DisplayTagg), but they have nothing to do with the action. You're simply setting some temporary Java variable to a value. When you use it, you need to de-reference it in scriptlet style (e.g., <%= name %>, etc)