controller
package com. gmy. controller ;
import com. gmy. pojo. User ;
import com. gmy. pojo. usertable ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Controller ;
import org. springframework. ui. Model ;
import org. springframework. web. bind. annotation. * ;
import org. thymeleaf. util. StringUtils ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpSession ;
import java. util. List ;
@Controller
public class LoginController {
@Autowired
com. gmy. service. usertableService usertableService;
@PostMapping ( "/login" )
@ResponseBody
public boolean Login ( @RequestBody User user, HttpServletRequest request) {
String pwd1 = user. getuPwd ( ) ;
String name = user. getuName ( ) ;
usertable userpwd = usertableService. getPwd ( name) ;
Integer code = userpwd. getuCode ( ) ;
HttpSession session = request. getSession ( ) ;
session. setAttribute ( "ucode" , code) ;
String pwd = userpwd. getuPwd ( ) ;
if ( name == null ) {
return false ;
}
if ( ! StringUtils . isEmpty ( name) && pwd. equals ( pwd1) ) {
return true ;
} else {
return false ;
}
}
@RequestMapping ( "/list" )
public String list ( Model model) {
List < usertable> users = usertableService. queryUserList ( ) ;
model. addAttribute ( "user" , users) ;
return "emp/list" ;
}
@RequestMapping ( "/add" )
public String adduser ( ) {
return "emp/add" ;
}
@PostMapping ( "/save" )
@ResponseBody
public boolean saveuser ( @RequestBody usertable user) {
System . out. println ( "添加" ) ;
int i = usertableService. addUser ( user) ;
if ( i> 0 ) {
return true ;
} else {
return false ;
}
}
@GetMapping ( "/update/{id}" )
public String update ( @PathVariable Integer id, Model model) {
System . out. println ( id) ;
System . out. println ( "修改" ) ;
usertable usertable = usertableService. queryUserById ( id) ;
model. addAttribute ( "user1" , usertable) ;
return "emp/modifie" ;
}
@PostMapping ( "/updateuser" )
@ResponseBody
public boolean updateUser ( @RequestBody usertable user) {
int i = usertableService. update ( user) ;
if ( i> 0 ) {
return true ;
} else {
return false ;
}
}
@RequestMapping ( "delete/{id}" )
public String delete ( @PathVariable Integer id, Model model) {
usertableService. deleteUserById ( id) ;
List < usertable> usertables = usertableService. queryUserList ( ) ;
model. addAttribute ( "user" , usertables) ;
return "emp/list" ;
}
@RequestMapping ( "xiangxi/{id}" )
public String xaingxi ( @PathVariable Integer id, Model model) {
usertable user = usertableService. queryUserById ( id) ;
System . out. println ( user) ;
model. addAttribute ( "userp" , user) ;
return "emp/xiangxii" ;
}
}
拦截器
package com. gmy. config ;
import org. springframework. web. servlet. HandlerInterceptor ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle ( HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object user = request. getSession ( ) . getAttribute ( "ucode" ) ;
String uri = request. getRequestURI ( ) ;
if ( user == null || uri != "/" ) {
response. sendRedirect ( "/" ) ;
return false ;
} else {
return true ;
}
}
}
映射
package com. gmy. config ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. web. servlet. config. annotation. InterceptorRegistry ;
import org. springframework. web. servlet. config. annotation. WebMvcConfigurer ;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors ( InterceptorRegistry registry) {
registry. addInterceptor ( new LoginInterceptor ( ) ) . addPathPatterns ( "/**" )
. excludePathPatterns ( "/" , "/login" ) ;
}
}