1.先照此创建文件
package com.zx.test.model; public class User { private Long id; private String name; private Integer age; private String password; // get/set方法 }
3。使用hibernate映射文件映射实体(User.hbm.xml)
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.zx.test.model.User" table="usertt" schema="dbo" catalog="good"> <id name="id" type="java.lang.Long"> <column name="id" /> <generator class="native"></generator> </id> <property name="name" type="java.lang.String"> <column name="name" length="20" /> </property> <property name="age" type="java.lang.Integer"> <column name="age" length="2" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="20" /> </property> </class> </hibernate-mapping>
4.编写dao层(UserDao.java)
package com.zx.test.dao; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.zx.test.model.User; public class UserDao extends HibernateDaoSupport { /** * 保存user * @param user */ public void saveUser(User user){ // spring自带方法 保存 this.getHibernateTemplate().save(user); } }
5.编写service层(UserService.java)
package com.zx.test.service; import com.zx.test.dao.UserDao; import com.zx.test.model.User; public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void saveUser(User user){ userDao.saveUser(user); } // 在此处添加业务逻辑方法; }
6.编写action层(UserAction.java)
package com.zx.test.action; import com.opensymphony.xwork2.ActionSupport; import com.zx.test.model.User; import com.zx.test.service.UserService; public class UserAction extends ActionSupport { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } private UserService userService ; public void setUserService(UserService userService) { this.userService = userService; } public String userAdd(){ this.userService.saveUser(user); return SUCCESS; } }
7.编写验证文件(UserAction-validation.xml)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name ="user.name" > <field-validator type="requiredstring" > <message >user.name string is required</message> </field-validator> <field-validator type="stringlength"> <param name="maxLength">10</param> <param name="minLength">5</param> <message> user.name is must between ${minLength} and ${maxLength} </message> </field-validator> </field> <field name ="user.password"> <field-validator type="requiredstring"> <message >user.password string is required</message> </field-validator> </field> <field name ="user.age"> <field-validator type="required"> <message>user.age string is required</message> </field-validator> <field-validator type="int"> <param name="max">150</param> <param name="min">5</param> <message>This is must between ${min} and ${max}</message> </field-validator> </field> </validators>
8.编写首页(index.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:form action="/user/userAdd.action" name="form1" method="post"> userName: <input type ="text" name="user.name"><br> password:<input type="password" name="user.password"><br> age:<input type="text" name="user.age"><br> <input type="submit" value="submit"> <input type="reset" value="reset"> <s:fielderror name="user.name"></s:fielderror> </s:form> </body> </html>
9.编写成功界面(userShow.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> userName: <s:property value="user.name"/><br> password:<s:property value="user.password"/><br> age:<s:property value="user.age"/> </body> </html>
查看整合(2)请点击连接 http://shaojiashuai123456.iteye.com/admin/blogs/474628