JSP输入数据(Welcome.Jsp)无法通过Java代码(OrderProcessingAction.saveOrUpdate())获取

sczxawaw  于 2023-10-14  发布在  Java
关注(0)|答案(2)|浏览(109)

JSP页面输入用户的详细信息并发送给struts action,struts.xml将actionMap到Action类OrderProcessingAction方法saveOrUpdate(),该方法调用DAO类的saveOrUpdateUser(),其中实际数据访问DB表字段。
以下是可交付成果:

  1. JSP页面**:Welcome.jsp
    1.联系我们
    OrderProcessingAction.java
    1.数据访问
    :**UserDaoFactory.java对象
  2. struts.xml文件
/**
 * Welcome.Jsp : Here my JSP page code.
 */
<s:form method="post" name="myform" action="saveOrUpdate.action">
                   
    <s:textfield label="Customer Name" name="username" size="30"/>
    
    <s:password label="Customer Password" name="password" size="30"/>
    
    <s:textfield label="Contact No" name="contact" size="30"/>
  
    <s:textfield label="Email" name="email" size="30"/>
   
    <s:textfield label="City" name="city" size="30"/>

                       
    <s:submit label="Submit" />
    <s:reset label="Clear"/>

</s:form>

这里是struts.xml文件。

<action name="saveOrUpdate" 
        class="com.view.OrderProcessingAction" 
        method="saveOrUpdate">
 <result name="success">/Welcome.jsp</result>
</action>

下面是我的OrderProcessingAction(ObjectClass),我在其中调用DAO类方法。

private String username;
private String password;
private int contact;
private String email;
private String city;
    
//Pojo class OrderPorcessing reference initiated
OrderProcessing user = new OrderProcessing();   

public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}   
public int getContact() {
    return contact;
}
public void setContact(int contact) {
    this.contact = contact;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
    
  /**
  * To save or update user. 
   * @return String
  */
   public String saveOrUpdate(){
            
    userDaoFactory.saveOrUpdateUser(user);
    System.out.println("User Name: " +user.getUsername());
    return SUCCESS;
  }

下面是DAO类中的saveOrUpdate方法。

/**
     * Used to save or update a user by saveOrUpdate().
     * @return void
     */
      public void saveOrUpdateUser(User user) {
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.getTransaction();
         
        session.saveOrUpdate(user);
        
        tx.commit();
        session.close();

    } catch (Exception e) {

        e.printStackTrace();
    }
}
wnvonmuf

wnvonmuf1#

而不是这样做

System.out.println("User Name: " +user.getUsername());

这么做

System.out.println("User Name: " +getUsername());
sqserrrh

sqserrrh2#

在代码中,应该将DAO替换为typical transaction
典型的事务应该使用以下习惯用法:

Session sess = factory.openSession();
 Transaction tx;
 try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
 }
 catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
 }
 finally {
     sess.close();
 }

相关问题