]999J2EE实验报告

时间:2024.3.19

1.4. 模块设计

1.完成ssh9

1)        创建数据库

2)        创建Web Project,名为ssh9

3)        添加Struts 2框架

4)        创建login.jsp

5)        创建LoginAction.java

6)        创建login_success.jsp

7)        创建login_error.jsp

8)        部署,测试struts2

2.集成Spring

9)        添加Sprin

10)     创建struts.properties

11)     修改struts.xml

12)     修改applicationContext.xml

13)     部署测试

3.加载Hibernate框架

14)     添加Spring框架

15)     修改LoginAction.java

16)     修改applicationContext.xml

17)     部署测试

1.5. 编码实现

数据库内容如下:

建表语句:

CREATE TABLE user(

id int(10) not null auto_increment,

username varchar(10) not null,

password varchar(10) not null,

primary key (id)

)ENGINE=InnoDB DEFAULT CHARSET=GBK;

其中已有数据:

LoginAction.java代码如下:

package org;

import java.util.List;

import org.hibernate.SessionFactory;

import org.hibernate.Query;

import org.hibernate.classic.Session;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

       private String username;

       private String password;

       private SessionFactory sessionFactory;

       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 SessionFactory getSessionFactory(){

              return sessionFactory;

       }

       public void setSessionFactory(SessionFactory sessionFactory){

              this.sessionFactory=sessionFactory;

       }

       public String execute() throws Exception{

              Session session=sessionFactory.openSession();

              String hql="from User u where u.username=? and u.password=?";

              Query query=session.createQuery(hql);

              query.setParameter(0,username);

              query.setParameter(1,password);

              List user=query.list();

              session.close();

              if(user.size()>0){

                     return SUCCESS;

              }

              else{

                     return ERROR;

              }

       }

}

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

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="org.User" table="user" catalog="test">

        <id name="id" type="java.lang.Integer">

            <column name="id" />

            <generator class="native" />

        </id>

        <property name="username" type="java.lang.String">

            <column name="username" length="10" not-null="true" />

        </property>

        <property name="password" type="java.lang.String">

            <column name="password" length="10" not-null="true" />

        </property>

    </class>

</hibernate-mapping>

User.hbm.xml代码如下:

package org;

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

       private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

    private  static Configuration configuration = new Configuration();

    private static org.hibernate.SessionFactory sessionFactory;

    private static String configFile = CONFIG_FILE_LOCATION;

       static {

           try {

                     configuration.configure(configFile);

                     sessionFactory = configuration.buildSessionFactory();

              } catch (Exception e) {

                     System.err

                                   .println("%%%% Error Creating SessionFactory %%%%");

                     e.printStackTrace();

              }

    }

    private HibernateSessionFactory() {

    }

    public static Session getSession() throws HibernateException {

        Session session = (Session) threadLocal.get();

              if (session == null || !session.isOpen()) {

                     if (sessionFactory == null) {

                            rebuildSessionFactory();

                     }

                     session = (sessionFactory != null) ? sessionFactory.openSession()

                                   : null;

                     threadLocal.set(session);

              }

        return session;

    }

       public static void rebuildSessionFactory() {

              try {

                     configuration.configure(configFile);

                     sessionFactory = configuration.buildSessionFactory();

              } catch (Exception e) {

                     System.err

                                   .println("%%%% Error Creating SessionFactory %%%%");

                     e.printStackTrace();

              }

       }

    public static void closeSession() throws HibernateException {

        Session session = (Session) threadLocal.get();

        threadLocal.set(null);

        if (session != null) {

            session.close();

        }

    }

       public static org.hibernate.SessionFactory getSessionFactory() {

              return sessionFactory;

       }

       public static void setConfigFile(String configFile) {

              HibernateSessionFactory.configFile = configFile;

              sessionFactory = null;

       }

       public static Configuration getConfiguration() {

              return configuration;

       }

}

LoginAction.java代码如下:

package org;

public class User implements java.io.Serializable {

       private Integer id;

       private String username;

       private String password;

       public User() {

       }

       public User(String username, String password) {

              this.username = username;

              this.password = password;

       }

       public Integer getId() {

              return this.id;

       }

       public void setId(Integer id) {

              this.id = id;

       }

       public String getUsername() {

              return this.username;

       }

       public void setUsername(String username) {

              this.username = username;

       }

       public String getPassword() {

              return this.password;

       }

       public void setPassword(String password) {

              this.password = password;

       }

}

配置struts.xml文件,代码如下:

<!DOCTYPE struts PUBLIC

       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

       "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

       <include file="struts-default.xml"/>

       <package name="default" extends="struts-default">

              <action name="login" class="org.LoginAction">

                     <result name="error">/login_error.jsp</result>

                     <result name="success">/login_success.jsp</result>

              </action>

       </package>

</struts>

创建login.jsp,代码如下:

<%@ page contentType="text/html;charset=gb2312" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>

  <head><title>登录页面</title></head>

  <body>

    <form action="login.action" method="post">

           用户登录<br>

           用户名:<input type="text" name="username"/><br>

           密码:<input type="password" name="password"/><br>

           <input type="submit" value="登录"/><br>

    </form>

  </body>

</html>

创建login_success.jsp,代码如下:

<%@ page contentType="text/html;charset=gb2312" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>

  <body>

    <h2>您好!用户<s:property value="username"/>欢迎您登录成功</h2>

  </body>

</html>

创建login_error.jsp,代码如下:

<%@ page contentType="text/html;charset=gb2312" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>

  <body>

    <h2>登录失败</h2>

  </body>

</html>

修改web.xml内容,使得程序增加对Spring的支持

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5"

       xmlns="http://java.sun.com/xml/ns/javaee"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

       <filter>

              <filter-name>struts2</filter-name>

              <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

       </filter>

       <filter-mapping>

              <filter-name>struts2</filter-name>

              <url-pattern>/*</url-pattern>

       </filter-mapping>

       <listener>

              <listener-class>

                     org.springframework.web.context.ContextLoaderListener

              </listener-class>

       </listener>

       <context-param>

              <param-name>contextConfigLocation</param-name>

              <param-value>

                     /WEB-INF/classes/applicationContext.xml

              </param-value>

       </context-param>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

使得struts 2的类(struts.properties)的生成交给Spring完成。

struts.objectFactory=spring

修改applicationContext.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

       <bean id="dataSource"

              class="org.apache.commons.dbcp.BasicDataSource">

              <property name="driverClassName"

                     value="com.mysql.jdbc.Driver">

              </property>

              <property name="url" value="jdbc:mysql://localhost:3306/test"></property>

              <property name="username" value="root"></property>

              <property name="password" value="kcw406880"></property>

       </bean>

       <bean id="sessionFactory"

              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

              <property name="dataSource">

                     <ref bean="dataSource" />

              </property>

              <property name="hibernateProperties">

                     <props>

                            <prop key="hibernate.dialect">

                                   org.hibernate.dialect.MySQLDialect

                            </prop>

                     </props>

              </property>

              <property name="mappingResources">

                     <list>

                            <value>org/User.hbm.xml</value></list>

              </property></bean>

              <bean id="login" class="org.LoginAction">

                     <property name="sessionFactory">

                            <ref bean="sessionFactory"/>

                     </property>

              </bean>

</beans>

3. 实验完成情况

程序运行:

输入后的结果:

完成的结果和预期一致,因此程序正确无误。

4. 实验总结

通过这次的实验我掌握了struts2,hibernate,spring技术的应用,使我有了基本的使用myeclipse来制作jsp网站的基础技术。

更多相关推荐:
B2C实验报告

一、实验目的1.了解B2C实验中各个角色的功能和任务2.掌握电子商务B2C系统的交易流程3.掌握电子商务B2C实验中角色的配合4.构建B2C交易流程,分析B2C交易中服务增值的环节二、实验要求1.了解B2C实验…

B2C实验报告

B2C实验报告,内容附图。

电子商务模拟B2C实验报告

电子商务B2C模拟实验报告实验概述电子商务实验教学系统B2C模式主要模拟电子商务B2C交易过程将B2C交易环境中相关要素灵活体现直观体验电子商务环境里角色的变化认识消费者销售部财务部经理储运部退货部采购部物流银...

B2C实验报告(实例)

装订线B2C实验报告第1页第3页第4页第5页第6页第7页第8页第9页线装订第10页第11页

B2C实验报告

实验报告课程名称电子商务实验名称实验一B2C模拟实验专业班级姓名学号实验日期20xx428第1页共14页第2页共14页第3页共14页第4页共14页第5页共14页第6页共14页第7页共14页第8页共14页第9页共...

B2C电子商务实验报告

实验目的及要求目的1了解B2C交易含义流程2各角色掌握自己角色的操作流程3掌握B2C交易中后台的详细处理过程通过实验了解及掌握B2C网上交易流程要求在本次实验中我们以一个小组的配合为主要实验方式组成独立商圈进行...

实验报告b2c

实验报告一实验名称B2C电子商务上机模拟演练二实验目的1熟悉电子商务B2C平台的交易环境2了解个人消费者如何在网上选择商品订购商品填写订单3了解电子商务网站的结构特点4理解电子商务B2C的交易模式5理解个人消费...

电子商务实验报告书-B2C模式

实验四B2C模式学号姓名成绩实验1消费者网上购物一实验目的通过前台操作熟练掌握网上交易的各个流程所选德意电子商务实验室的模块网上交易之二实验内容熟悉电子商务网站的结构功能查询和选择购买商品注册成为新会员会员信息...

B2C电子商务 学生实验报告

附件1中北大学经济与管理学院实验报告课程名称电子商务学号0809044126学生姓名夏菊美辅导教师马力我院任课教师有实验课的均要求有实验报告每个实验项目要求有一份实验报告实验报告按照格式书写完毕后经辅导实验的教...

电子商实验报告B2C C2C

电子商务概论实验报告一学院商学院专业财务会计年级20xx学号20xx244132姓名黄静梅实验题目一实验目的1熟悉C2C电子商务网站的页面布局排版风格和功能特色等2了解典型C2C电子商务网站的购物流程3掌握C2...

电子商务b2c实验报告

1网上购物1当当网购物流程首先进入当当网主页点击网页左上角的免费注册如果有账号可直接点击登录如图所示填好后点击提交注册然后要进行邮箱验证验证结束便可以购物了通过首页的搜索栏或者直接在分类中查找可找到自己所要的物...

电子商务实验报告B2C

装订电子商务实验报告一第1页第2页第3页第4页第6页第7页第8页

b2c实验报告(21篇)