Spring mvc + hibernate Annotation Version test add, delete, modify, query simple xml configuration application
Spring mvc is easy to use. I have been studying spring mvc for a while. I really don't think I like to compare spring mvc with struts2. In terms of development efficiency, I feel that struts 2 is not fast, there are powerful annotation functions, but it is difficult to debug and maintain annotations for large projects! When talking about xml configuration, I inserted it! It's too complicated and hard to understand. At most, struts1 Is Not up and down, and struts2 is used to using wildcards. It's too slow to use this method! Maybe I have not found any better spring mvc configuration!
Some code:
Xml
ContextConfigLocation
Classpath: config/config-dao.xml classpath: config/config-sevice.xml
Org. springframework. web. context. ContextLoaderListener
SpringMvc
Org. springframework. web. servlet. DispatcherServlet
ContextConfigLocation
Classpath: config/config-springServlet.xml
1
SpringMvc
*. Do
CharacterEncodingFilter
Org. springframework. web. filter. CharacterEncodingFilter
Encoding
UTF-8
ForceEncoding
True
CharacterEncodingFilter
/*
Index. jsp
Config-springServlet.xml
config-dao.xml
Org. hibernate. dialect. MySQLDialect
Update
True
Classpath: config/hibernate. cfg. xml
UserController. java
Package org. controller; import java. util. list; import javax. annotation. resource; import javax. servlet. http. httpServletRequest; import org. bean. user; import org. service. service; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. context. webApplicationContext; import org. springframework. web. context. support. webApplicationContextUtils; import org. springframework. web. portlet. modelAndView; import org. springframework. web. servlet. support. requestContextUtils; @ Controllerpublic class UserController {// userController @ Resource (name = userService) private Service userService; @ RequestMapping (/spirng/helloSpring) public String helloSpring (HttpServletRequest request) {// spring context @ SuppressWarnings (unused) WebApplicationContext ac1 = WebApplicationContextUtils. getWebApplicationContext (request. getSession (). getServletContext (); // spring mvc context WebApplicationContext ac2 = RequestContextUtils. getWebApplicationContext (request); System. out. println (userService. helloSpring (); return/show;} // add user @ RequestMapping (/userdemo/addUser) public String addUser (User user, HttpServletRequest request) {System. out. println (user. getUsername () + user. getAge (); try {userService. add (user);} catch (Exception e) {// TODO: handle exception return error;} List
UserList = userService. getAll (); request. setAttribute (userList, userList); return userList;} // select user @ RequestMapping (/userdemo/userList) public String showUserList (HttpServletRequest request) {List
UserList = userService. getAll (); System. out. println (userList. size (); request. setAttribute (userList, userList); return userList;} // show edit uesr @ RequestMapping (/userdemo/showEditUser) public String showEditUser (String uuid, HttpServletRequest request) {User user = userService. getSingleUser (uuid); request. setAttribute (user, user); return editUser;} // edit user @ RequestMapping (/userdemo/editUser) public String editUser (User uesr, HttpServletRequest request) {try {userService. update (uesr);} catch (Exception e) {return error;} List
UserList = userService. getAll (); request. setAttribute (userList, userList); return userList;} // delete user @ RequestMapping (/userdemo/deleteUser) public String deleteUser (String uuid, HttpServletRequest request) {System. out. println (ddd); try {userService. delete (userService. getSingleUser (uuid);} catch (Exception e) {return error;} List
UserList = userService. getAll (); request. setAttribute (userList, userList); return userList ;}}
UserList. jsp
<%@ page language=java import=java.util.* pageEncoding=UTF-8%><%@ taglib prefix=c uri=http://java.sun.com/jsp/jstl/core%><%String path = request.getContextPath();String basePath = request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;%>
> <Script type = text/javascript src = js/jquery-1.9.1.js> </script> Add User List
| T name |
Age |
Operation |
| $ {TempList. username} |
$ {TempList. age} |
Edit and delete |
<Script type = text/javascript> // edit function enditUser (id) {location. href =/springmvc2/userdemo/showEditUser. do? Uuid = + id;} // delete function deleteUser (id) {location. href =/springmvc2/userdemo/deleteUser. do? Uuid = + id ;}</script>