• struts 2 实现表单传送数据到数据库


    表单 。jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>注册</title>
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <script src="jQuery/jquery-1.7.2.min.js"></script>
    <script>
     function validate(){
      
      var password=document.lform.password.value;
      var psw2=document.lform.psw2.value;
       if(psw2 != password){
       alert("两次密码不一致,请重新输入");
       document.lform.password.focus();//光标定位在文本框acccountName中
       return false;
      }
      return true;
     }

    </script>
    </head>
    <body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="./">jsp作业</a>
            </div>
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="login.html">登录</a></li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container">
        <div class="row">
            <div class="col-md-4">
            </div>
            <div class="col-md-4">
                <form class="form-signin" action="userloggin.action" name="lform"onSubmit="return validate()">
                    <h2 class="form-signin-heading">欢迎使用bbs系统!</h2>
                    <div id="info"></div>
                    <label for="inputacount">账号</label>
                    <input type="text" name="useraccount" class="form-control" placeholder="请输入账号" required autofocus><br>
                    <label for="Password">密码</label>
                    <input type="password" name="password" class="form-control" placeholder="请输入密码" required maxLength="16"><br>
                    <label for="Password">重复密码</label>
                    <input type="password" name="psw2" class="form-control" placeholder="请再次输入密码" required maxLength="16"><br>
                    <input type="submit" class="btn btn-primary"  value="注册">
                   <a href="login.html" class="btn btn-default" id="btn-reg">返回登录</a>
                </form>
                <iframe style="display: none;" name="submitFrame" src="about:blank"></iframe>
            </div>
            <div class="col-md-4">
            </div>
        </div>
        <script src="js/jquery.min.js"></script>
    </body>
    </html>

    struts 2 action 配置

    <package name="luck" extends="struts-default" namespace="/">
        <action name="userloggin" class="com.ftt.Action.UserLoginAction">
        <result name="ok">/main.jsp</result>
        </action>
        </package>

    插入到数据库相关代码

    package com.ftt.Action;

    import javax.print.attribute.standard.Severity;
    import javax.servlet.http.HttpServletRequest;

    import java.sql.PreparedStatement;
    import java.util.Map;
    import org.apache.struts2.ServletActionContext;
    import org.apache.struts2.interceptor.RequestAware;
    import com.ftt.model.*;
    import com.ftt.db.DbConnect;
    import com.opensymphony.xwork2.ActionSupport;
    public class UserLoginAction extends ActionSupport implements RequestAware{
     private String useraccount;
     private String password;
     
     public String getPassword() {
      return password;
     }
     public void setPassword(String password) {
      this.password = password;
     }
     @Override
     public void setRequest(Map<String, Object> arg0) {
      // TODO Auto-generated method stub
      
     }

     public String execute() throws Exception{
      
      
      HttpServletRequest request = ServletActionContext.getRequest();
    //  String  userpickname = request.getParameter("userpickname");
     // String  password=request.getParameter("password");  
      System.out.println(useraccount+" "+password);
       ConnectStatic.conn= DbConnect.getDbConnect();
       String sql="insert into usermanager(useraccount,password,userpickname,useremailaddress, "
         + "usersex,userinterests,userimageaddress,"
         + "userlogintime,userbirthday,"
         + "usergignature,userqq,work) value(?,?,'noname',null,null,null,null,now(),null,null,null,null)";
       // Connection conn ;
        PreparedStatement pstmt= ConnectStatic.conn.prepareStatement(sql);
        request.setCharacterEncoding("utf-8");
          
           String posttitle=request.getParameter("userpickname");
        
           String postcontent=request.getParameter("password");
    //       useraccount=null;
    //       pstmt.setString(1, useraccount);
    //       pstmt.setString(3, userpickname);
           pstmt.setString(1, useraccount);
           pstmt.setString(2, password);
          
          
        
         
           int n=pstmt.executeUpdate();
           if(n==1){System.out.println("数据插入成功!");
          
            }
           
             else{System.out.println("数据插入失败!");}
             if(pstmt!=null){pstmt.close();}
             if(ConnectStatic.conn!=null){ConnectStatic.conn.close();}
             return "ok";
      
     }
     public String getUseraccount() {
      return useraccount;
     }
     public void setUseraccount(String useraccount) {
      this.useraccount = useraccount;
     }


    }

  • 相关阅读:
    Linux下Redis集群环境的搭建
    redis: 6379端口下set值时出现 CLUSTERDOWN The cluster is down
    运行Maven工程总是报错:No goals have been specified for this build
    Centos6.5 配置Nginx开机自启动
    centos6.5无法访问网络
    centos6.5 nginx-1.8.0和ftp搭建图片服务器
    HTML中使图片居中显示
    使用InstallAnywhere工具打包Java_Web程序
    Android开发中Parcelable接口的使用方法
    Java8并行流使用注意事项
  • 原文地址:https://www.cnblogs.com/www-hsy-com/p/8298579.html
Copyright © 2020-2023  润新知