【发布时间】:2015-09-21 07:56:01
【问题描述】:
我正在为我的 Web 应用程序使用 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 中自动装配服务,或者我认为将数据传递给视图层而不是获取它很好在视图层?
【问题讨论】:
标签: java spring jsp servlets autowired