jsp:UseBean与Struts< bean>标签 [英] jsp:UseBean vs. Struts <bean> tag
问题描述
我正在学习Struts2,但我还不清楚如何以及何时使用标签<jsp:UseBean>
与Struts标签.
I am learning about Struts2 and it's not completely clear to me how and when to use the tag <jsp:UseBean>
vs the Struts tag.
我找到了一篇文章,概述了这些差异,但它们之间却没有提供任何示例.
I found an article outlining the differences but them but there is not any example provided.
http://struts.apache.org/development/1.x/struts-taglib/tlddoc/bean/define.html
<bean:define>
标记与<jsp:useBean>
有所不同,包括:
The <bean:define>
tag differs from <jsp:useBean>
in several ways, including:
- 在指定的条件下无条件创建(或替换)bean 标识符.
- 可以使用属性返回的值创建一个bean 不同bean的getter(包括用a引用的属性 嵌套和/或索引属性名称).
- 可以创建一个bean,其 content是文字字符串(或运行时表达式的结果) 由value属性指定.
- 不支持嵌套内容 (例如标记),仅当Bean被执行时才执行 实际创建的.
- Unconditionally creates (or replaces) a bean under the specified identifier.
- Can create a bean with the value returned by a property getter of a different bean (including properties referenced with a nested and/or indexed property name).
- Can create a bean whose contents is a literal string (or the result of a runtime expression) specified by the value attribute.
- Does not support nested content (such as tags) that are only executed if a bean was actually created.
这是我使用UseBean标记的快速测试代码.谁能给我一个例子,为什么/为什么(或者是否)应该在display.jsp中使用Struts标记?
Here's my quick test code that uses the UseBean tag. Could anyone please give me an example how/why (or if) I should use the Struts tag(s) in the display.jsp instead?
谢谢!
index.jsp
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="input" action="TestServlet" method="post">
<input type="text" name="txtFirstName" placeholder="First Name">
<input type="text" name="txtLastName" placeholder="Last Name">
<input type="submit" value="Submit">
</form>
</body>
</html>
TestServlet.java
TestServlet.java
package com.website.servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.website.models.Person;
/**
* Servlet implementation class TestServlet
*/
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fname = request.getParameter("txtFirstName");
String lname = request.getParameter("txtLastName");
Person p = new Person();
p.setFirstName(fname);
p.setLastName(lname);
request.setAttribute("myPersonObj", p);
RequestDispatcher rd = request.getRequestDispatcher("display.jsp");
rd.forward(request, response);
}
}
display.jsp
display.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="myPersonObj" class="com.website.models.Person" scope="request"></jsp:useBean>
<p>
Full Name : <jsp:getProperty property="firstName" name="myPersonObj"/>
<jsp:getProperty property="lastName" name="myPersonObj"/>
</p>
</body>
</html>
推荐答案
由于您实际上是在servlet中创建bean,并将其放入request属性中,因此可以替换:
Because you are actually creating the bean in the servlet, and putting it in a request attribute, you can replace:
<jsp:useBean id="myPersonObj" class="com.website.models.Person" scope="request"></jsp:useBean>
<p>
Full Name : <jsp:getProperty property="firstName" name="myPersonObj"/>
<jsp:getProperty property="lastName" name="myPersonObj"/>
</p>
使用(使用EL):
<p>
Full Name : ${myPersonObj.firstName}
${myPersonObj.lastName}
</p>
在SO上查看 Servlets信息页面,此处:
如果您已经在使用servlet处理模型,请不要使用
<jsp:useBean>
.这只会导致混乱和维护麻烦,因为<jsp:useBean>
遵循的MVC方法级别与使用servlet时不同.是servlet或<jsp:useBean>
,不是两者.
Do NOT use
<jsp:useBean>
if you're already using a servlet to process the model. It will only lead to confusion and maintenance trouble because the<jsp:useBean>
follows a different level of MVC approach than when you're using servlets. It's either servlets or<jsp:useBean>
, not both.
因为<jsp:useBean>
与servlet不兼容,并且您需要使用servlet进行任何体面的编码,所以<jsp:useBean>
是完全过时的"(如JB Nizet在第一条评论中所述).
Because <jsp:useBean>
is not compatible with servlets, and you need to use servlets to do any decent coding, <jsp:useBean>
is "completely obsolete" (as JB Nizet put it in the first comment).
这篇关于jsp:UseBean与Struts< bean>标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!