zoukankan      html  css  js  c++  java
  • 利用hibernate与struts框架制作简单注册界面

    一:配置hibernate
      1.导包 hibernate包和jdbc连接mysql数据库的包
      2.实用工具生成hibernate配置文件和映射文件
      3.做好hibernateUtil生成session的静态单例配置文件
    二:编写数据访问

    package com.dao;
    
    import java.util.List;
    
    import org.hibernate.Session;
    
    import com.model.HibernateUtil;
    import com.model.Login;
    
    public class LoginDao {
        Session session = null;
        public LoginDao(){
            session =HibernateUtil.getSession();
        }
        public List<Login> select(String name){
            List<Login> list = null;
            try{
            list = session.createQuery("from Login where UserName = ?")
                    .setParameter(0, name)
                    .getResultList();
            }
            catch(Exception e){
                e.getStackTrace();
            }
            finally{
                HibernateUtil.closeSession();
            }
            return list;
        }
        public void add(Login l){
            try{
            session.beginTransaction();
            session.save(l);
            session.getTransaction().commit();
            }
            catch(Exception e){
                e.getStackTrace();
                session.getTransaction().rollback();
            }
            finally{
                HibernateUtil.closeSession();
            }
        }
    }

    三:配置struts

    在apps文件夹下将struts-blank.war复制粘贴将后缀改为rar格式,解压到这个目录下,这样就生成了一个空白的项目,这里边的WEB-INF文件下lib里边的jar包就是我们struts所需要必须的jar包,另外如果需要用jstl标签,就把jstl的jar包也导入我们项目的WEB-INF文件下的lib中

    2.配置web.xml()

     这里的web.xml一样是刚刚生成的空白项目中的WEB-INF文件下的空白web.xml文件,复制粘贴到项目中,将我们不需要用的删除即可,只留下我们需要的内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_9" 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">
     
        <display-name>Struts Blank</display-name>
     
        <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.html</welcome-file>
        </welcome-file-list>
     
         
     
    </web-app>

    四、做界面

    reg.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!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>
        <form action="reg" method="post">
            账号:<input type="text" name="username" ><br>
            昵称:<input type="text" name="name"><br>
            密码:<input type="password" name="password" ><br>
            金额:<input type="text" name="account">
            <input type="submit" value="提交">
        </form>
        <form action="ttt">
        123123
        <input type="submit" value="提交">
        </form>
    </body>
    </html>

    index_add.jsp:

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!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>
        <form action="Index_insert" method="post">
            账号:<input type="text" name="username" ><br>
            昵称:<input type="text" name="name"><br>
            密码:<input type="password" name="password" ><br>
            金额:<input type="text" name="account">
            <input type="submit" value="提交">
        </form>
    </body>
    </html>

    配置IndexAction.java文件

    package com.controller;
    
    import java.math.BigDecimal;
    import java.util.List;
    
    import com.dao.LoginDao;
    import com.model.Login;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class IndexAction extends ActionSupport {
        private String username;
        private String name;
        private String password;
        private BigDecimal account;
        
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public BigDecimal getAccount() {
            return account;
        }
        public void setAccount(BigDecimal account) {
            this.account = account;
        }
        public String add(){
            return SUCCESS;
        }
        public String insert(){
            return SUCCESS;
        }
        @Override
        public String execute() throws Exception {
            
            
            List<Login> list=new LoginDao().select(username);
            if(list.size()==0){
                Login l = new Login();
                l.setUserName(username);
                l.setName(name);
                l.setPassword(password);
                l.setAccount(account);
                new LoginDao().add(l);
                return SUCCESS;
            }
            else{
                return ERROR;
            }
                
            
            
        }
    }

    配置struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    
        <constant name="struts.enable.DynamicMethodInvocation" value="false" />
        <constant name="struts.devMode" value="true" />
    
        <package name="default" namespace="/" extends="struts-default">
             
            <action name="reg" class="com.controller.IndexAction">//直接在地址栏运行reg.jsp,然后跳转reg到IndexAction下的execute方法,成功走reg_ok.jsp
                <result name="success">
                    reg_ok.jsp
                </result>
                <result name="error">
                    reg_error.jsp
                </result>
            </action>
            
            <!--
            <action name="Index_add" class="com.controller.IndexAction" method="add">//直接在IndexAction中写入add与insert方法,地址栏直接输入Index_add跳转到add界面
    在Index_add.jsp中提交到Index_insert,然后走下面的方法来实现功能 <result> Index_add.jsp </result> </action> <action name="Index_insert" class="com.controller.IndexAction" method="insert"> <result> Index_insert.jsp </result> </action> <action name="*_*" class="com.controller.{1}Action" method="{2}">//这个方法是最简单的,前面的*代表上文所写的Index,后面的代表在IndexAction中缩所写的跳转的方法
    可以免去上文那么麻烦的写法,只写一个action即可解决所有问题, <result> {1}_{2}.jsp </result> </action>
    --> </package> </struts>

    运行起来输入action中的*_*的地址即可显示出来。

  • 相关阅读:
    Springsecurity3.1.3配置多个登陆页面
    将数字转换为大写(保留小数点后面2位)
    纯JavaScript实现的二维码图片生成器
    poi导出excel
    发送邮件
    Lodop实现打印功能
    遍历list
    循环监听输入框回车事件
    监听回车事件记录
    简单的事务操作过程
  • 原文地址:https://www.cnblogs.com/claricre/p/6545493.html
Copyright © 2011-2022 走看看