1、新增页面上有必输项、可选项,保存时我们只需要写一个insert的SQL即可,该SQL要包含
页面上所有的必输项和可选项,那么对于可选项中没有输入的值,将按照null来插入。
2、有二次请求
请求1,打开新增页面
请求2,保存
代码:
--DAO
--Action
--struts.xml
Action:
struts.xml
Jsp:
页面上所有的必输项和可选项,那么对于可选项中没有输入的值,将按照null来插入。
2、有二次请求
请求1,打开新增页面
请求2,保存
代码:
--DAO
--Action
--struts.xml
--JSP
DAO:
public void insert(Account account) throws DAOException {
String sql = "insert into account values(account_seq.nextval,?,?,?,'0',sysdate,null,null, "+
"?,?,?,?,?,?,?,?,?,?,null,null)";
Connection con = DBUtil.getConnection();
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setObject(1, account.getRecommenderId());
ps.setObject(2, account.getLoginName());
ps.setObject(3, account.getLoginPassword());
ps.setObject(4, account.getRealName());
ps.setObject(5, account.getIdcardNo());
ps.setObject(6, account.getBirthDate());
ps.setObject(7, account.getGender());
ps.setObject(8, account.getOccupation());
ps.setObject(9, account.getTelephone());
ps.setObject(10, account.getEmail());
ps.setObject(11, account.getMailaddress());
ps.setObject(12, account.getZipcode());
ps.setObject(13, account.getQq());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException("查询账务账号失败", e);
} finally{
DBUtil.close();
}
}
Action:
public class AddAccountAction {
public String execute(){
IAccountDAO dao = DAOFactory.getAccountDAO();
try {
dao.insert(account);
} catch (DAOException e) {
e.printStackTrace();
return "error";
}
return "success";
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
//input
private Account account;
}
struts.xml
<!-- 打开新增页面 -->
<action name="toAddAccount">
<result name="success">
/WEB-INF/account/addAccount.jsp
</result>
</action>
<!-- 新增Action -->
<action name="addAccount" class="netctoss.action.account.AddAccountAction">
<result name="success" type="redirectAction">
findAccount
</result>
</action>
Jsp:
<!--findAccount.jsp-->
<input type="button" value="增加" class="btn_add" οnclick="location.href='toAddAccount';" />
<!--addAccounnt.jsp-->
<form action="addAccount" method="post" class="main_form">
<!--必填项-->
<div class="text_info clearfix"><span>姓名:</span></div>
<div class="input_info">
<input type="text" name="account.realName" />
<span class="required">*</span>
<div class="validate_msg_long">20长度以内的汉字、字母和数字的组合</div>
</div>
<div class="text_info clearfix"><span>身份证:</span></div>
<div class="input_info">
<input type="text" name="account.idcardNo" id="idcardNo"
οnblur="checkIdcardNo()"/>
<span class="required">*</span>
<div class="validate_msg_long" id="idcardNoMsg">正确的身份证号码格式</div>
</div>
<div class="text_info clearfix"><span>登录账号:</span></div>
<div class="input_info">
<input type="text" name="account.loginName" />
<span class="required">*</span>
<div class="validate_msg_long">30长度以内的字母、数字和下划线的组合</div>
</div>
<div class="text_info clearfix"><span>密码:</span></div>
<div class="input_info">
<input type="password" name="account.loginPassword"/>
<span class="required">*</span>
<div class="validate_msg_long">30长度以内的字母、数字和下划线的组合</div>
</div>
<div class="text_info clearfix"><span>重复密码:</span></div>
<div class="input_info">
<input type="password" />
<span class="required">*</span>
<div class="validate_msg_long">两次密码必须相同</div>
</div>
<div class="text_info clearfix"><span>电话:</span></div>
<div class="input_info">
<input type="text" class="width200" name="account.telephone"/>
<span class="required">*</span>
<div class="validate_msg_medium">正确的电话号码格式:手机或固话</div>
</div>
<!--可选项-->
<div class="text_info clearfix"><span>可选项:</span></div>
<div class="input_info">
<img src="../images/show.png" alt="展开" οnclick="showOptionalInfo(this);" />
</div>
<div id="optionalInfo" class="hide">
<div class="text_info clearfix"><span>推荐人身份证号码:</span></div>
<div class="input_info">
<input type="text"/>
<!-- 推荐人ID,当营业员输入完推荐人身份证后,异步查询出其ID -->
<input type="hidden" name="account.recommenderId"/>
<div class="validate_msg_long">正确的身份证号码格式</div>
</div>
<div class="text_info clearfix"><span>生日:</span></div>
<div class="input_info">
<input type="text" name="account.birthdate" id="birthdate" class="readonly" />
</div>
<div class="text_info clearfix"><span>Email:</span></div>
<div class="input_info">
<input type="text" class="width350" name="account.email"/>
<div class="validate_msg_tiny">50长度以内,合法的 Email 格式</div>
</div>
<div class="text_info clearfix"><span>职业:</span></div>
<div class="input_info">
<select name="account.occupation">
<option value="">请选择</option>
<option value="0">干部</option>
<option value="1">学生</option>
<option value="2">技术人员</option>
<option value="3">其他</option>
</select>
</div>
<div class="text_info clearfix"><span>性别:</span></div>
<div class="input_info fee_type">
<input type="radio" name="account.gender" value="1" checked="checked" id="female" />
<label for="female">女</label>
<input type="radio" name="account.gender" value="0" id="male" />
<label for="male">男</label>
</div>
<div class="text_info clearfix"><span>通信地址:</span></div>
<div class="input_info">
<input type="text" class="width350" name="account.mailaddress"/>
<div class="validate_msg_tiny">50长度以内</div>
</div>
<div class="text_info clearfix"><span>邮编:</span></div>
<div class="input_info">
<input type="text" name="account.zipcode"/>
<div class="validate_msg_long">6位数字</div>
</div>
<div class="text_info clearfix"><span>QQ:</span></div>
<div class="input_info">
<input type="text" name="account.qq"/>
<div class="validate_msg_long">5到13位数字</div>
</div>
</div>
<!--操作按钮-->
<div class="button_info clearfix">
<input type="submit" value="保存" class="btn_save" />
<input type="button" value="取消" class="btn_save" οnclick="history.go(-1);"/>
</div>
</form>