Java Web实验 Spring架构的实现
立即下载
资源介绍:
Java Web项目初步学习
package com.controller;
import com.dao.UserDao;
import com.dao.UserDaoImpl;
import com.entity.User;
import com.service.UserService;
import com.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
public class UserController extends HttpServlet{
private UserService userService;
@Override
public void init()throws ServletException{
//使用WebApplicationContextUtils 获取Spring容器
ApplicationContext context= WebApplicationContextUtils.getWebApplicationContext(getServletContext());
userService=(UserService)context.getBean("userService");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
try{
if("query".equals(action)){
queryUsers(request, response);
} else if ("add".equals(action)) {
addUser(request, response);
} else if ("delete".equals(action)) {
deleteUser(request, response);
} else if ("update".equals(action)) {
updateUser(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Database error");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
private void addUser(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ServletException {
int id= Integer.parseInt(request.getParameter("id"));
String username=request.getParameter("username");
String pwd=request.getParameter("pwd");
userService.addUser(id,username,pwd);
request.getRequestDispatcher("/user.jsp").forward(request, response);
response.sendRedirect("user?action=query");
}
private void queryUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
List users = userService.getAllUsers();
request.setAttribute("users", users);
request.getRequestDispatcher("/user.jsp").forward(request, response);
}
private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ServletException, ServletException {
String id = request.getParameter("id");
userService.deleteUser(id);
request.getRequestDispatcher("/user.jsp").forward(request, response);
response.sendRedirect("user?action=query");
}
private void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ServletException {
int id = Integer.parseInt(request.getParameter("id"));
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
userService.updateUser(id, username, pwd);
request.getRequestDispatcher("/user.jsp").forward(request, response);
response.sendRedirect("user?action=query");
}
}
资源文件列表:
bmms_spring.zip 大约有149个文件