Hibernate【4】——与Struts2整合
1、整体目录
struts负责响应前台页面的动作;
hibernate负责后台链接数据库的操作;
(1)添加struts包到lib
(2)添加struts的xml配置文件
(3)添加Action类
2、struts的xml
只需要写<action>标签之间的东西
23 <action name="login" class="com.demo.struts.action.UserAction" method="login"> 24 <result name="success">/success.jsp</result> 25 <result name="error">/index.jsp</result> 26 </action>
name和index页面中的action的name一样,class表示使用那个action类来响应,method表示使用action类中的哪个方法
result的name表示method返回的值,/success.jsp表示对应result跳转的页面
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 9 <constant name="struts.devMode" value="true" /> 10 11 <package name="base" namespace="/" extends="struts-default"> 12 13 <default-action-ref name="index" /> 14 15 <global-results> 16 <result name="error">/error.jsp</result> 17 </global-results> 18 19 <global-exception-mappings> 20 <exception-mapping exception="java.lang.Exception" result="error"/> 21 </global-exception-mappings> 22 23 <action name="login" class="com.demo.struts.action.UserAction" method="login"> 24 <result name="success">/success.jsp</result> 25 <result name="error">/index.jsp</result> 26 </action> 27 28 <action name="register" class="com.demo.struts.action.UserAction" method="register"> 29 <result name="register">/register.jsp</result> 30 </action> 31 32 <action name="register2" class="com.demo.struts.action.UserAction" method="register2"> 33 <result name="register">/register.jsp</result> 34 <result name="regsucc">/regsucc.jsp</result> 35 </action> 36 </package> 37 38 39 <!-- Add packages here --> 40 41 </struts>
3、Action类
关于数据库的操作,只要调用service层的类就好了~
1 package com.demo.struts.action; 2 3 import com.demo.model.User; 4 import com.demo.service.UserService; 5 import com.demo.service.UserServiceImpl; 6 import com.opensymphony.xwork2.ActionSupport; 7 8 public class UserAction extends ActionSupport { 9 private User user; 10 private String confirmPwd; 11 private String message; 12 private UserService userService = new UserServiceImpl(); 13 14 /** 15 * 用户登录 16 * @return 17 */ 18 public String login(){ 19 String name = user.getUserName(); 20 String pwd = user.getUserPwd(); 21 if(userService.login(name, pwd)!=null){ 22 message ="登录成功!"; 23 return "success"; 24 }else{ 25 message = "用户名或者密码错误!"; 26 return "error"; 27 } 28 } 29 /** 30 * 跳转注册页面 31 * @return 32 */ 33 public String register(){ 34 return "register"; 35 } 36 37 /** 38 * 用户注册 39 * @return 40 */ 41 public String register2(){ 42 String name=user.getUserName(); 43 String pwd = user.getUserPwd(); 44 if(userService.isExist(name, pwd) == true){ 45 return "register"; 46 }else{ 47 userService.add(user); 48 return "regsucc"; 49 } 50 } 51 52 53 54 public User getUser() { 55 return user; 56 } 57 58 public void setUser(User user) { 59 this.user = user; 60 } 61 62 public String getMessage() { 63 return message; 64 } 65 66 public void setMessage(String message) { 67 this.message = message; 68 } 69 70 public String getConfirmPwd() { 71 return confirmPwd; 72 } 73 74 public void setConfirmPwd(String confirmPwd) { 75 this.confirmPwd = confirmPwd; 76 } 77 78 }
![字节跳动内推](https://img2020.cnblogs.com/blog/323808/202004/323808-20200415172728571-1992447742.jpg)
找我内推: 字节跳动各种岗位
作者:
ZH奶酪(张贺)
邮箱:
cheesezh@qq.com
出处:
http://www.cnblogs.com/CheeseZH/
*
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。