JSP(JavaWeb必备)

@配置文件:web.cml   struts.xml   hibernate.cfg.xml   hibernate映射文件如userInfo.hbm.xml   必须创建HibernateSessionFactory.java(右击项目MyEclipse-Add Hibernate Capabilities创建)@

 

@流程:jsp - Action(方法调(传个参数)) - Server层(调Dao层的方法) - DAO层(连接数据库,实现操作数据库中数据的一些方法)@

 

@web.xml@

<?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>
  <!-- 过滤器支持的struts2类  -->
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</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>

 

 

 

@struts.xml@

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <package name="achievement_hibernate" extends="struts-default">      
 
  <!-- 查询所有学生信息的Action -->
  <action name="query" class="action.query" method= "">
   <result name="input">/query/selectAll_button.jsp</result>
   <result name="success">/query/selectOk.jsp</result>
  </action>

 </package>
</struts>

 

 

 

@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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
 <property name="connection.username">root</property>
 <property name="connection.url">
  jdbc:mysql://localhost:3306/produce
 </property>
 <property name="dialect">
  org.hibernate.dialect.MySQLDialect
 </property>
 <property name="connection.password">111</property>
 <property name="connection.driver_class">
  com.mysql.jdbc.Driver
 </property>
 <property name="hbm2ddl.aut">update</property>
 <property name="connection.autocommit">true</property>
 <property name="show_sql">true</property>
 <mapping resource="produce/userInfo.hbm.xml" />
 <mapping resource="produce/produceInfo.hbm.xml" />
 <mapping resource="produce/foreignKey.hbm.xml" />

</session-factory>

</hibernate-configuration>

 

 

 

@userInfo.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 table="user_info" name="produce.UserInfo">
  <id name="uid">
   <generator class="identity"></generator>
  </id>
  <property name="u_name"></property>
  <property name="remark"></property>
  <one-to-many name=""></one-to-many>
 </class>
</hibernate-mapping>

 

 

@Action中的selectAll()方法调server层中的query()方法,在query()方法里调DAO层中的query_all()方法@

#selectAll():#

 public String selectAll()
 {
  rs = curd.query();
  Relation re = (Relation)rs;
  System.out.print("Uinfo:" + re.getUid() + re.getU_name() + re.getRemark());
  System.out.print("");
  System.out.print("Pinfo:" + re.getPid() + re.getP_name() + re.getPrice());
   return "success";
 }

#query():#

/**
 * @server层里的一些增删改查功能方法
 */

package server;

import java.util.List;

import action.query;

import produce.ForeignKey;
import produce.ProduceInfo;
import produce.UserInfo;

import dao.ForeignDao;

 

public class CURD {  
 /**
  * select all from foreignkey table,by foreignkey uid and by foreignkey pid,from two table
  */
 public List query()
 {
  List result = new ForeignDao().query_all(); 
  return result;
 }

}

#query_all():#

public class ForeignDao {
 public List query_all()
 {
  //Configuration 管理 Hibernate 配置
  Configuration config = new Configuration().configure();
  //根据Configuration 建立 SessionFactory,SessionFactory 用来建立Session
  SessionFactory sessionFactory = config.buildSessionFactory();
  Session session = sessionFactory.openSession();
  
  List result = session.createQuery("select u.uid,u.u_name,u.remark,p.pid,p.p_name,p.price from produce.UserInfo as u,produce.ProduceInfo as p,produce.ForeignKey as fkey where fkey.uid=u.uid and fkey.pid=p.pid").list();
  session.close();
  return result;
 }

}

 

 

#jsp页面#

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

   <s:form method="POST" action="query!selectAll">
    <s:submit value="查询"></s:submit>
    </s:form>

 

显示页面:

  <tr>
   <s:iterator value="re">
    <s:property value="uid"/>
    <s:property value="u_name"/>
   </s:iterator> 
  </tr>
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值