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;
 }


}

 

posted @ 2018-01-16 20:13  红树叶  阅读(623)  评论(0编辑  收藏  举报