Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从JSP中的业务逻辑获取bean(使用表达式语言)_Jsp_El - Fatal编程技术网

从JSP中的业务逻辑获取bean(使用表达式语言)

从JSP中的业务逻辑获取bean(使用表达式语言),jsp,el,Jsp,El,我有一个JSP,它接受一个参数(用户ID),通过scriptlet中的业务逻辑调用检索用户详细信息;然后,我在各种表单字段中显示检索到的详细信息: <%@page import="temp.UserLogic"%> <%@page import="temp.User"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

我有一个JSP,它接受一个参数(用户ID),通过scriptlet中的业务逻辑调用检索用户详细信息;然后,我在各种表单字段中显示检索到的详细信息:

<%@page import="temp.UserLogic"%>
<%@page import="temp.User"%>
<%@ 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>
<%
    User user = new UserLogic().getUser(Long.parseLong(request.getParameter("userId")));
%>
</head>
<body>
    <form action="">
        <label>ID:</label>
        <input type="text" value="<%= user.getId() %>">
        <label>Username:</label>
        <input type="text" value="<%= user.getUsername() %>">
        <label>Password:</label>
        <input type="text" value="<%= user.getPassword() %>">
    </form>
</body>
</html>
我已经看到了useBean标记,如果我只想实例化和填充一个新的bean,那么它非常棒,但是在这个例子中,我希望这个bean来自业务逻辑


这种模式一定很常见,但我花了好几个小时在网上搜寻,却找不到答案。。。这里的任何帮助都将不胜感激。

业务逻辑bean——UserLogic——在JSP中没有任何位置。这应该在后端代码中调用,然后这个后端代码应该将用户放在JSP的作用域中

MVC(Model-View-Controller)模式正是您所需要的

在Servlet中,这将类似于:

//Servlet Acting as Controller
public LoadUserServlet extends HttpServlet{

    public void doGet(HttpServletRequest request, HttpServletResponse response){

        //Controller Loads the Model
        User user = new UserLogic().getUser(Long.parseLong(request.getParameter("userId")));
        request.setAttribute("user", user);

        //Controller Forwards to the Next View
        rd = request.getRequestDispatcher("/userEdit.jsp").forward(request, response);
    }
}
然后,您的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 action="">
        <label>ID:</label>
        <input type="text" value="${user.id}">
        <label>Username:</label>
        <input type="text" value="${user.userName}">
        <label>Password:</label>
        <input type="text" value="${user.password}">
    </form>
</body>
</html>

在此处插入标题
身份证件:
用户名:
密码:

当然,可能有Java MVC框架可以简化使用原始servlet时所需的大量锅炉板代码

“业务逻辑bean——UserLogic——在JSP中没有任何位置。应该在后端代码中调用它,然后这个后端代码应该将用户放在JSP的作用域中”。那么这是否意味着我需要使用servlet来服务我的“查看用户”请求,然后从servlet转发到JSP(用户bean现在在范围内)?是的:查看MVC模式(模型-视图-控制器)。例如,你看:如果我有权力,我会投赞成票!谢谢你的帮助,艾伦·海伊。
//Servlet Acting as Controller
public LoadUserServlet extends HttpServlet{

    public void doGet(HttpServletRequest request, HttpServletResponse response){

        //Controller Loads the Model
        User user = new UserLogic().getUser(Long.parseLong(request.getParameter("userId")));
        request.setAttribute("user", user);

        //Controller Forwards to the Next View
        rd = request.getRequestDispatcher("/userEdit.jsp").forward(request, response);
    }
}
<%@ 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 action="">
        <label>ID:</label>
        <input type="text" value="${user.id}">
        <label>Username:</label>
        <input type="text" value="${user.userName}">
        <label>Password:</label>
        <input type="text" value="${user.password}">
    </form>
</body>
</html>