使用spring

时间:2015-09-21 07:56:02

标签: java spring jsp servlets autowired

我在我的网络应用程序中使用spring(不是mvc),servlet,jsp,我想在jsp页面上显示用户列表,怎么做?这是我的代码

LoginService.java
@Service
public class LoginService {

    @PersistenceContext
    EntityManager em;

    public User getUserByUserName(String userName, String password) {
        User user = null;
        try {
            user = em.createQuery("from User u where u.userName = :userName and u.password = :password", User.class)
                    .setParameter("userName", userName)
                    .setParameter("password", password)
                    .getSingleResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    public List<User> getListOfUsers() {
        return em.createQuery("from User u", User.class).getResultList();
    }
}
LoginServlet.java
@Component
public class LoginServlet extends HttpServlet {

    @Autowired
    LoginService loginService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        EntityManagerFactory emf = (EntityManagerFactory) request.getServletContext().getAttribute("emf");
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        User user = loginService.getUserByUserName(userName, password);
        if(user != null){
            request.getSession().setAttribute("user", user);
            response.sendRedirect("home.jsp");
        }
        else{
            response.sendRedirect("login.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
Login.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>Login Page</title>
</head>
<body>
<form id="form" name="form" method="post" action="login">
    <h1>Login</h1>
    Please enter your login information
    <br/>New User? <a href="register.jsp">Register</a>
    Enter your user ID
    <input type="text" name="userName" id="userId"/>
    Password
    <input type="password" name="password" id="password"/>
    <button type="submit">Sign-in</button>
</form>
</body>
</html>
Home.jsp
<%@page import="demo.spring.entity.User" %>
<%@page import="java.util.Date" %>
<%@ page import="java.util.List" %>
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>Home Page</title>
</head>
<body>
<h1>User</h1>
<form id="form" name="form" method="post" action="update">
<p>
    <%=new Date()%></br>
    <%
        User user = (User) session.getAttribute("user");
    %>
    <b>Welcome <%= user.getFirstName() + " " + user.getLastName()%>
    </b>
    <br/>
    <a href="logout.jsp">Logout</a>
</p>

<table>
    <thead>
    <tr>
        <th>User ID</th>
        <th>First Name</th>
        <th>Middle Name</th>
        <th>Last Name</th>
        <th>email</th>
    </tr>
    </thead>
    <%--<tbody>
        <%
            LoginService loginService = new LoginService();
            List<User> list = loginService.getListOfUsers();
            for (User u : list) {
        %>
            <tr>
                <td><input type="text" name="firstName" id="firstName" value="<%=u.getUserName()%>"/></td>
                <td><%=u.getFirstName()%></td>
                <td><%=u.getMiddleName()%></td>
                <td><%=u.getLastName()%></td>
                <td><%=u.getEmail()%></td>
            </tr>
        <%}%>
    <tbody>--%>
</table>
<br/>
</form>

</body>
</html>
请告诉我,正确的方法是什么?如果我的代码是正确的,或者在设计方面需要进行任何修改,还是正确的做法,请提出建议?

以前我曾经在home.jsp中创建一个LoginService对象,但这不是正确的方法,我需要在jsp中自动装配服务,或者我认为如果我将数据传递给视图层而不是获取它是好的在视图层?

1 个答案:

答案 0 :(得分:0)

jsp页面中的Java代码被认为是不好的做法。使用Spring,您应该创建一个使用Controllers来与其他组件交互,并将要显示的信息添加到传递给jsp的模型中进行渲染。有关详细信息,请参阅Spring MVC reference documentation