1. 程式人生 > >家庭記賬管理系統的登陸和添加功能

家庭記賬管理系統的登陸和添加功能

pass pen 找不到 創建 urn ssp rac 越界 記得

一聲要走多遠的路程

夢想需要多少時間

今天又帶薪搞了一天的畢設,主要實現了登陸和添加信息的功能

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>

服務器啟動後,添加跳轉後,數據庫添加成功即可。

家庭記賬管理系統的登陸和添加功能