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