如何在struts文本框中显示bean值

时间:2013-06-21 16:57:41

标签: struts

您好我想使用struts2在文本框中显示bean值,但它无法正常工作,请帮忙。 即使我试图设置并从bean中获取值,但仍然无法正常工作。 我附上了输出。

Customer.java 


package com.java.bean;

 public class Customer {
private int id;
private String customerName;
private String cifNumber;
private int idNumber;
private String idCountry;
private String idType;
private int master_id;
private String rmCode;
private String customerCountry;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getCustomerName() {
    return customerName;
}
public void setCustomerName(String customerName) {
    this.customerName = customerName;
}
public String getCifNumber() {
    return cifNumber;
}
public void setCifNumber(String cifNumber) {
    this.cifNumber = cifNumber;
}
public int getIdNumber() {
    return idNumber;
}
public void setIdNumber(int idNumber) {
    this.idNumber = idNumber;
}
public String getIdCountry() {
    return idCountry;
}
public void setIdCountry(String idCountry) {
    this.idCountry = idCountry;
}
public String getIdType() {
    return idType;
}
public void setIdType(String idType) {
    this.idType = idType;
}
public int getMaster_id() {
    return master_id;
}
public void setMaster_id(int master_id) {
    this.master_id = master_id;
}
public String getRmCode() {
    return rmCode;
}
public void setRmCode(String rmCode) {
    this.rmCode = rmCode;
}
public String getCustomerCountry() {
    return customerCountry;
}
public void setCustomerCountry(String customerCountry) {
    this.customerCountry = customerCountry;
}



}

CustomerSearchActionForam.java

      package com.vaannila;

      public class CustomerSearchActionForm extends org.apache.struts.action.ActionForm{
private int id;
private String customerName;
private String cifNumber;
private int idNumber;
private String idCountry;
private String idType;
private int master_id;
private String rmCode;
private String customerCountry;
private String message;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getCustomerName() {
    return customerName;
}
public void setCustomerName(String customerName) {
    this.customerName = customerName;
}
public String getCifNumber() {
    return cifNumber;
}
public void setCifNumber(String cifNumber) {
    this.cifNumber = cifNumber;
}
public int getIdNumber() {
    return idNumber;
}
public void setIdNumber(int idNumber) {
    this.idNumber = idNumber;
}
public String getIdCountry() {
    return idCountry;
}
public void setIdCountry(String idCountry) {
    this.idCountry = idCountry;
}
public String getIdType() {
    return idType;
}
public void setIdType(String idType) {
    this.idType = idType;
}
public int getMaster_id() {
    return master_id;
}
public void setMaster_id(int master_id) {
    this.master_id = master_id;
}
public String getRmCode() {
    return rmCode;
}
public void setRmCode(String rmCode) {
    this.rmCode = rmCode;
}
public String getCustomerCountry() {
    return customerCountry;
}
public void setCustomerCountry(String customerCountry) {
    this.customerCountry = customerCountry;
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}


          }

SearchCustomer.java

import javax.servlet.http.HttpServletRequest;
import java.beans.*;
import java.util.List;
import java.util.Iterator;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.util.MessageResources;
import org.hibernate.HibernateException; 
import org.hibernate.Query;
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.java.bean.Customer;
import com.java.bean.LoginUser;

public class SearchCustomer extends org.apache.struts.action.Action {
@Override

public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    CustomerSearchActionForm customerSearch = (CustomerSearchActionForm) form;
    MessageResources msgResource = getResources(request);
    String cifNumber_=customerSearch.getCifNumber();
    String Result_ = "success";
    System.out.println("CIF NUMBER:"+cifNumber_);

    SearchCustomer Scust = new SearchCustomer();
    List customer=Scust.listCustomers(cifNumber_);
    request.setAttribute("customerList",customer);
    Iterator iterator = customer.iterator(); 
    if(iterator.hasNext()){
        System.out.println("customer found");

        customerSearch.setMessage(msgResource.getMessage("msg.customerfound"));
    }
    else{
        System.out.print("Customer Not Found");
        customerSearch.setMessage(msgResource.getMessage("error.nocustomerfound"));
    }
    System.out.println("List of Customers : "+customer);
    return mapping.findForward(Result_);    
}
public List listCustomers(String cifNumber_){
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    List customer = null;
    try{
         tx = session.beginTransaction();
         Query sql = session.createQuery("from Customer where cifNumber=:cifNumbervalue");
         System.out.println("Query printing "+sql);
         sql.setParameter("cifNumbervalue",cifNumber_);
         customer = sql.list();
         System.out.println("CUSTOMER LIST ITERATION"+customer);

       for (Iterator iterator = customer.iterator(); iterator.hasNext();){
            Customer cust = (Customer) iterator.next(); 
            String cifNumber=cust.getCifNumber();
            String custName=cust.getCustomerName();
            System.out.println("-----------------------------------------------------------     - -------------------------------------------");
            System.out.print("CifNumber: " + cifNumber+"   CustomerName :"+custName); 
         }
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
return customer;
    }

}

LoginHome.jsp(从数据库显示我的记录)

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<div style="color:red">
    <html:errors />
</div>
<html:form action="/Login" >
    User Name :<html:text name="LoginActionForm" property="userName" />
    Password  :<html:password name="LoginActionForm" property="passWord" />
    <html:submit value="Login" />
</html:form>

</body>

输出

CifNumber: 10000 

CustomerName: JP Morgon 

IDNumber: 123321 

ID Country: SINGAPORE 

RM CODE: E001 

CustomerCountry: USA 



Customer Details

CIF NUMBER :  
CUSTOMER NAME :  
ID NUMBER :  
ID COUNTRY :  
RM CODE :  
CUSTOMER COUNTRY :  
login 

0 个答案:

没有答案