jsp:useBean call a specific method in a class

六眼飞鱼酱① 提交于 2020-01-03 00:02:57

问题


How can I call a specific method in a class using this tag?

<jsp:useBean id="user" scope="??" class="com.example.User" type="com.example.User" />


回答1:


Assuming your bean User has a method called getName()

<jsp:useBean id="user" scope="request" class="com.example.User" />
// ...
<h1>Hello <jsp:getProperty name="user" property="name" /></h1>

The scope could be something else than request : depends on what you want (session, page, etc)


EDIT: your second question was about calling a business method in your jsp

The fact is, you should not call business method into your JSPs. Your JSP pages should only display static (html, etc) and dynamic (beans for example) content. If you follow MVC pattern, business job is delegated to servlets. Your JSPs are only simple views (reading properties of beans) and are forbidden (in directory WEB-INF).

For your JSP pages, proceed as following:

  1. Action of html form is your servlet controller (method POST)
  2. Add hidden input to help servlet recognize what to do: <input type="hidden" name="action" value="update" /> or <input type="hidden" name="action" value="register" />

You can also display dynamic content (beans). For that, you should consider using JSTL.
Example (servlet sends an ArrayList of Movie beans on request scope):

<c:forEach items="${requestScope.results}" var="movie">
   // ${movie.title}
   // etc
</c:forEach>

For your servlet controller, proceed as following:

In doPost method (because we're using POST method in html form), you can dispatch business logic depending on action received: request.getParameter("action")

If action is update, then we perform update business logic.
If action is register, then we perform register business logic.
etc ...

Now, we can store some data in request scope to communicate to jsp page:

ArrayList<Movie> results = new ArrayList<Movie>();
results.add(new Movie(...));
// etc...
request.setAttribute("results", results);

And finally, send data and display jsp:

request.getRequestDispatcher("/WEB-INF/update.jsp").forward(request, response);

An example of web.xml (with a servlet mapped as welcome file index.jsp)

<servlet>
    <servlet-name>Search</servlet-name>
    <servlet-class>Servlets.Search</servlet-class>
</servlet>
<servlet>
    <servlet-name>Register</servlet-name>
    <servlet-class>Servlets.Register</servlet-class>
</servlet>
<servlet>
    <servlet-name>Update</servlet-name>
    <servlet-class>Servlets.Update</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Search</servlet-name>
    <url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Register</servlet-name>
    <url-pattern>/Register.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Update</servlet-name>
    <url-pattern>/Update.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>



回答2:


<jsp:useBean id="user" scope="request" class="com.example.User" type="com.example.User" />
<jsp:setProperty name="user" property="*" />
<c:set var="saveStatus" value="${user.save()}" />


来源:https://stackoverflow.com/questions/21258739/jspusebean-call-a-specific-method-in-a-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!