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
标签与
有几个不同之处,包括:
The <bean:define>
tag differs from <jsp:useBean>
in several ways, including:
- 无条件地创建(或替换)指定的bean标识符.
- 可以使用属性返回的值创建bean不同 bean 的 getter(包括用嵌套和/或索引的属性名称).
- 可以创建一个 bean内容是文字字符串(或运行时表达式的结果)由 value 属性指定.
- 不支持嵌套内容(例如标签)仅在 bean 被执行时才执行实际创建.
这是我使用 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
<%@ 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,并且把它放在一个请求属性中,你可以替换:
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 上查看 Servlet 信息页面:
如果您已经在使用 servlet 来处理模型,请不要使用
.它只会导致混乱和维护麻烦,因为
遵循与使用 servlet 时不同级别的 MVC 方法.它要么是 servlet,要么是
,而不是两者.
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>
与servlets不兼容,你需要使用servlets来做任何像样的编码,
是完全过时了"(正如 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 &lt;bean>标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!