BustedSanta
BustedSanta

Reputation: 1388

jsp:UseBean vs. Struts <bean> tag

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 bean:... 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:

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 bean:... 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:

    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:

    <%@ 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>

Upvotes: 0

Views: 6762

Answers (2)

Roman C
Roman C

Reputation: 1

You should use struts2 tags, they has support for OGNL. OGNL has a context and a value stack which is a root object. So, your request scoped object is displayed as

<p> 
    Full Name : <s:property value="#request.myPersonObj.firstName"/>&nbsp;
                <s:property value="#request.myPersonObj.lastName"/>
</p>

For better understanding OGNL concepts see docs.

Upvotes: 1

developerwjk
developerwjk

Reputation: 8659

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>

With (using EL):

    <p> 
        Full Name : ${myPersonObj.firstName}
                    ${myPersonObj.lastName}
    </p>

See the Servlets info page here on SO:

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.

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).

Upvotes: 3

Related Questions