一声要走多远的路程
梦想需要多少时间
今天又带薪搞了一天的毕设,主要实现了登陆和添加信息的功能
1.先说登陆:
1.1前端界面copy,记得把登录名和密码的name改的和后面一致
1.2UserAction中实现登陆方法,使用属性封装的方式获取前端表单中的名字与密码
1 package cn.action; 2 3 import javax.servlet.http.HttpServletRequest; 4 5 6 import org.apache.struts2.ServletActionContext; 7 8 import com.opensymphony.xwork2.ActionSupport; 9 10 import cn.entity.User; 11 import cn.service.UserService; 12 13 public class UserAction extends ActionSupport { 14 private UserService userService; 15 16 public void setUserService(UserService userService) { 17 this.userService = userService; 18 } 19 //属性封装方式 20 private String username; 21 private String password; 22 public String getUsername() { 23 return username; 24 } 25 26 public void setUsername(String username) { 27 this.username = username; 28 } 29 30 public String getPassword() { 31 return password; 32 } 33 34 public void setPassword(String password) { 35 this.password = password; 36 } 37 38 39 //登陆方法 40 public String login() { 41 //封装到实体类对象 42 User user=new User(); 43 user.setUsername(username); 44 user.setPassword(password); 45 //调用service的方法实现 46 User userExist= userService.login(user); 47 //判断 48 if(userExist!=null) {//成功 49 //使用session保持登陆状态 50 HttpServletRequest request= ServletActionContext.getRequest(); 51 request.getSession().setAttribute("user",userExist); 52 return "loginSuccess"; 53 }else {//shibai 54 return "login"; 55 } 56 57 58 } 59 60 }
1.3service中实现登陆方法
public User login(User user) {
// 调用doo里面的方法
return userDao.loginUser(user);
}
调用dao
1.4dao中
User loginUser(User user);
1。5daoImpl,dao的实体类中实现从数据库中查询是否有这个人的存在
1 package cn.dao; 2 3 4 import java.util.List; 5 6 import org.springframework.orm.hibernate5.HibernateTemplate; 7 import org.springframework.orm.hibernate5.support.HibernateDaoSupport; 8 9 import cn.entity.User; 10 //HibernateDao 11 public class UserDaoImdl extends HibernateDaoSupport implements UserDao{ 12 // private HibernateTemplate hibernateTemplate; 13 // 14 //public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { 15 // this.hibernateTemplate = hibernateTemplate; 16 //} 17 //登陆方法 18 public User loginUser(User user) { 19 //调用方法到hibernate Template对象 20 21 HibernateTemplate hibernateTemplate = this.getHibernateTemplate(); 22 //登陆的查询操作 23 //根据用户名和密码查询 24 @SuppressWarnings("all") 25 List<User> list=(List<User>) this.getHibernateTemplate(). 26 find("from User where username=? and password=?",user.getUsername(),user.getPassword()); 27 //返回user对象 28 29 //如果查询之后,没有结果,list里面没有值,get(下标)找不到,出现下标越界异常 30 //判断 31 if(list.size()!=0&&list !=null) { 32 User u= list.get(0); 33 return u; 34 } 35 return null; 36 } 37 38 39 }
方法要继承hibernatedaosupport从而实现hibernate的模板对象
1.6 struts.xml文档中修改
<action name="user_*" class="userAction" method="{1}">
<result name="loginSuccess">index.html</result>
<result name="login">login.jsp</result>
</action>
登陆成功和失败跳转的界面
2开始实现添加功能
成功之后跳转首页,首页点添加按钮后,实现跳转添加界面,输入信息,添加到后端数据库中
2.1创建CustomerAction类,注入service并实现跳转界面
private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
//1.添加页面
public String toAddPage() {
return "toAddPage";
}
2.2创建customerservice类,注入dao
public class CustomerService {
private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
2.3创建dao的接口和其实现类
2.4 spring的配置文件中引入customer的配置文件,实现模块开发
spring配置文件中添加<import resource="classpath:customer.xml"></import>
2.5创建customer.xml文件实现各种注入功能
<bean id="customerAction" class="cn.action.CustomerAction" scope="prototype" >
<property name="customerService" ref="customerService"></property>
</bean>
<bean id="customerService" class="cn.service.CustomerService">
<property name="customerDao" ref="customerDaoImpl"></property>
</bean>
<bean id="customerDaoImpl" class="cn.dao.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
2.6修改struts.xml文件实现点击跳转(添加下面语句)
<action name="customer_*" class="customerAction" method="{1}">
<!-- 到添加的页面 -->
<result name="toAddPage">/jsp/customer/add.jsp</result>
</action>
2.7开始添加功能
2.7.1customerAction加入添加的方法
//2.添加方法
public String add()
{
//添加逻辑
customerService.add(customer);
return "add";
}
2.7.2service中
public void add(Customer customer) {
customerDao.add(customer);
}
2.7.3dao中
void add(Customer customer);
2。7.4 dao的实现类中调用hibernate的模板功能
public void add(Customer customer) {
this.getHibernateTemplate().save(customer);
}
2.7.5创建一个customer的实体类,使用模块驱动的方式实现添加
private Integer cid;
private String custName;
private String custLevel;
private String custSource;
private String custPhone;
private String custMobile;
get和set方法
2.7.6新建一个 customer.hbm.xml文档(类似user.hbm.xml)注意类名
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.entity.Customer" table="t_customer"> <id name="cid" column="uid"> <generator class="native"/> </id> <property name="custName" column="username"/> <property name="custLevel" column="custLevel"/> <property name="custSource" column="custSource"/> <property name="custPhone" column="custPhone"/> <property name="custMobile" column="custMobile"/> </class> </hibernate-mapping>
2.7.7整体的hibernate.xml文档中添加一句映射
<mapping resource="cn/entity/Customer.hbm.xml"/>
2.7.8 struts.xml文档中实现添加成功跳转的界面
<!-- 添加成功之后 -->
<result name="add">/jsp/success.jsp</result>
服务器启动后,添加跳转后,数据库添加成功即可。
原文:https://www.cnblogs.com/stt-ac/p/10644663.html