百度的各种代码,步骤,自己整合了一下
1,创建数据库
常用mysql creat table.....
2,在WebContent下的bin中添加相应的包
http://pan.baidu.com/s/1c2DR2co
本人的百度云盘分享
3,创建实体类以及相应的映射文件、
例如stuinfo.java和stuinfo.hbm.xml
stuinfo.java中包含,私有成员对象和getter,setter方法
package Po; public class Stuinfo implements java.io.Serializable{
private String id;
private String name;
private String sex;
private int age;
private float weight;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
} }
stuinfo.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">
<!-- Generated 2011-12-9 12:17:31 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="PO.Stuinfo" table="stuinfo" catalog="test">
<id name="id" type="string">
<column name="id" length="20" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="name" length="20" not-null="true" />
</property>
<property name="sex" type="string">
<column name="sex" length="5" not-null="true" />
</property>
<property name="age" type="int">
<column name="age" not-null="true" />
</property>
<property name="weight" type="float">
<column name="weight" precision="10" scale="0" not-null="true" />
</property>
</class>
</hibernate-mapping>
4,配置hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<mapping resource="PO/Stuinfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5,创建相应的action
即各种Java动作类以及配置方法
配置文件: <result>中,当调用方法成功后,如果返回值与name中的字符串相同,就会跳转相应的页面
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default"> <action name="lookMessageAction" class="studentAction.LookMessageAction">
<result name="success">/student/lookMessage.jsp</result>
<result name="input">/student/index.jsp</result>
</action>
<action name="addMessageAction" class="studentAction.AddMessageAction">
<result name="success" type="chain">lookMessageAction</result>
<result name="input">/student/addMessage.jsp</result>
</action>
<action name="findMessageAction" class="studentAction.FindMessageAction">
<result name="success">/student/updateMessage.jsp</result>
<result name="input">/student/findMessage.jsp</result>
</action>
<action name="updateMessageAction" class="studentAction.UpdateMessageAction">
<result name="success" type="chain">lookMessageAction</result>
<result name="input">/student/updateMessage.jsp</result>
</action>
<action name="deleteMessageAction" class="studentAction.DeleteMessageAction">
<result name="success" type="chain">lookMessageAction</result>
<result name="input">/student/deleteMessage.jsp</result>
</action> </package>
</struts>
相应的Java方法
package studentAction; import Dao.StudentDao;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext; public class LookMessageAction extends ActionSupport{
private HttpServletRequest request;
private String message="input";
public String execute() throws Exception{
request=ServletActionContext.getRequest();
StudentDao dao=new StudentDao();
List list=dao.findAllInfo();
request.getSession().setAttribute("count", list.size());
request.getSession().setAttribute("allInfo", list);
message="success";
return message;
}
}
6,在web.xml中配置启动Struts2框架的过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
7,编写显示界面jsp文件