JSP页面输入用户的详细信息并发送给struts action,struts.xml
将actionMap到Action类OrderProcessingAction
方法saveOrUpdate()
,该方法调用DAO类的saveOrUpdateUser()
,其中实际数据访问DB表字段。
以下是可交付成果:
- JSP页面**:Welcome.jsp
1.联系我们:OrderProcessingAction.java
1.数据访问:**UserDaoFactory.java对象 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();
}
}
2条答案
按热度按时间wnvonmuf1#
而不是这样做
这么做
sqserrrh2#
在代码中,应该将DAO替换为typical transaction
典型的事务应该使用以下习惯用法: