关于struts的set get方法

chinoxl 2013-02-01 10:11:50
最近在做一个当当网项目


有一个前台注册页面传输数据给后台action


我记得前台传输数据给后台action只调用 set方法吧

但是去掉get方法后一直报空指针

不知道什么原因


package org.tarena.dang.action.user;

import org.tarena.dang.action.BaseAction;
import org.tarena.dang.dao.JdbcUserDAO;
import org.tarena.dang.dao.UserDAO;
import org.tarena.dang.entity.User;
import org.tarena.dang.util.Constant;
import org.tarena.dang.util.EmailUtil;
import org.tarena.dang.util.EncryptUtil;
import org.tarena.dang.util.VerifyUtil;

public class RegisterAction extends BaseAction{
//input
private User user;
//output

public String execute() {


//密码加密

System.out.println(user.getNickname());
System.out.println(user.getPassword());
System.out.println(user.getEmail());
String pwd = EncryptUtil.md5Encrypt(
user.getPassword());


user.setPassword(pwd);
//设置用户初始信息
user.setUserIntegral(Constant.NORMAL);
user.setEmailVerify(Constant.NO);
user.setLastLoginTime(
System.currentTimeMillis());
user.setLastLoginIp(httpReq.getRemoteAddr());
//生成一个邮箱验证码
String code = VerifyUtil.createCode();
user.setEmailVerifyCode(code);
//将user写入d_user
UserDAO userDao = new JdbcUserDAO();
try {
userDao.save(user);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//发送邮箱验证码
EmailUtil.sendEmail(user.getEmail(), code);

return "verify";//进入邮箱验证页面
}



public void setUser(User user) {
this.user = user;
}






}
...全文
171 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinoxl 2013-02-01
  • 打赏
  • 举报
回复
测试下来 前台jsp页面传参数到后台action 是要调用action中getuser方法的
chinoxl 2013-02-01
  • 打赏
  • 举报
回复
用struts2的 user属性set get都设置了 user类中的属性名和你页面中<input name="user.xxxx" .../> 也是一样的 前台user中参数 测试下来只有password传输过来 但是把getUser这个方法加上后 就没问题了 但前台传参数到后台 我记得是用不到get方法吧
求知路漫漫 2013-02-01
  • 打赏
  • 举报
回复
你用的是struts1? 你的确定你的user类中的属性都有标准的set/get方法,另外确定你的user类中的属性名和你页面中<input name="user.xxxx" .../> 是一样的。
xxyifan 2013-02-01
  • 打赏
  • 举报
回复
看错误,跟get/set方法没什么关系,是SQL执行错误,看看email是不是为空?
a597926661 2013-02-01
  • 打赏
  • 举报
回复
引用 5 楼 kyouyoufei 的回复:
null说的是这个java.sql.SQLException: Column 'email' cannot be null 你数据库那张表表email列不能为空,就是你插入数据时user.email为空了
你把这个字段设为非空 在插入的时候后台user对象没获取到email属性的值 就报空指针异常了 这个跟get set方法没关系吧 楼主最好断点跟踪看一下email的在哪消失的 有没有进入后台
chinoxl 2013-02-01
  • 打赏
  • 举报
回复
email字段没问题啊 而且设为非空 现在主要问题是后台action接收不到前台 参数
CharlotteKong 2013-02-01
  • 打赏
  • 举报
回复
null说的是这个java.sql.SQLException: Column 'email' cannot be null 你数据库那张表表email列不能为空,就是你插入数据时user.email为空了
peterhero211 2013-02-01
  • 打赏
  • 举报
回复
你数据库字段emaill是设置的is null,不为空吗! 然后确定一下字段名称!
chinoxl 2013-02-01
  • 打赏
  • 举报
回复
不好意思 前端页面写错了 是这个 <%@page contentType="text/html;charset=utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>用户注册 - 当当网</title> <link href="../css/login.css" rel="stylesheet" type="text/css" /> <link href="../css/page_bottom.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="../js/jquery-1.4.3.js"></script> <script type="text/javascript"> var email_flag = false; var nickname_flag = false; //表单校验 $(function(){ //邮箱检验 $("#txtEmail").blur(function(){ email_flag = false; //清空提示消息 $("#email\\.info").html(""); //1.非空检查 var email = $("#txtEmail").val(); if(email == ""){ $("#email\\.info").html("邮箱不能为空"); return; } //2.格式检查 var pattern=/\b(^['_A-Za-z0-9-]+(\.['_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))$)\b/; if(!pattern.test(email)){ $("#email\\.info").html("邮箱格式错误"); return; } //3.唯一性检查 $.post( "checkEmail.action", {"email":email}, function(data){//获取返回的ok值 if(data){//可用 $("#email\\.info").html("邮箱可用"); email_flag = true;//邮箱通过检测 }else{//被占用 $("#email\\.info").html("邮箱被占用"); } } ); }); }); //表单提交事件处理 $(function(){ $("#register_form").submit(function(){ //判断各个表单项是否通过检查 // return email_flag&&nickname_flag&&; if(!email_flag){ alert("表单信息有错或正在检测中..."); return false; } return true; }); }); </script> </head> <body> <%@include file="../common/head1.jsp"%> <div class="login_step"> 注册步骤: <span class="red_bold">1.填写信息</span> > 2.验证邮箱 > 3.注册成功 </div> <div class="fill_message"> <form name="ctl00" method="post" action="register.action" id="register_form"> <h2> 以下均为必填项 </h2> <table class="tab_login" > <tr> <td valign="top" class="w1"> 请填写您的Email地址: </td> <td> <input name="user.email" type="text" id="txtEmail" class="text_input"/> <div class="text_left" id="emailValidMsg"> <p> 请填写有效的Email地址,在下一步中您将用此邮箱接收验证邮件。 </p> <span id="email.info" style="color:red"></span> </div> </td> </tr> <tr> <td valign="top" class="w1"> 设置您在当当网的昵称: </td> <td> <input name="user.nickname" type="text" id="txtNickName" class="text_input" /> <div class="text_left" id="nickNameValidMsg"> <p> 您的昵称可以由小写英文字母、中文、数字组成, </p> <p> 长度4-20个字符,一个汉字为两个字符。 </p> <span id="name.info" style="color:red"></span> </div> </td> </tr> <tr> <td valign="top" class="w1"> 设置密码: </td> <td> <input name="user.password" type="password" id="txtPassword" class="text_input" /> <div class="text_left" id="passwordValidMsg"> <p> 您的密码可以由大小写英文字母、数字组成,长度6-20位。 </p> <span id="password.info" style="color:red"></span> </div> </td> </tr> <tr> <td valign="top" class="w1"> 再次输入您设置的密码: </td> <td> <input name="password1" type="password" id="txtRepeatPass" class="text_input"/> <div class="text_left" id="repeatPassValidMsg"> <span id="password1.info" style="color:red"></span> </div> </td> </tr> <tr> <td valign="top" class="w1"> 验证码: </td> <td> <img class="yzm_img" id='imgVcode' src="#" /> <input name="number" type="text" id="txtVerifyCode" class="yzm_input"/> <div class="text_left t1"> <p class="t1"> <span id="vcodeValidMsg">请输入图片中的四个字母。</span> <span id="number.info" style="color:red"></span> <a href="#" >看不清楚?换个图片</a> </p> </div> </td> </tr> </table> <div class="login_in"> <input id="btnClientRegister" class="button_1" name="submit" type="submit" value="注 册"/> </div> </form> </div> <%@include file="../common/foot1.jsp"%> </body> </html>
chinoxl 2013-02-01
  • 打赏
  • 举报
回复
<%@page contentType="text/html;charset=utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>生成订单 - 当当网</title> <link href="../css/login.css" rel="stylesheet" type="text/css" /> <link href="../css/page_bottom.css" rel="stylesheet" type="text/css" /> </head> <body> <%@include file="../common/head1.jsp"%> <div class="login_step"> 生成订单骤: 1.确认订单 > <span class="red_bold"> 2.填写送货地址</span> > 3.订单成功 </div> <div class="fill_message"> <p> 选择地址: <select id="address"> <option> 填写新地址 </option> </select> </p> <form name="ctl00" method="get" action="order_ok.jsp" id="f"> <input type="hidden" name="id" id="addressId" /> <table class="tab_login"> <tr> <td valign="top" class="w1"> 收件人姓名: </td> <td> <input type="text" class="text_input" name="receiveName" id="receiveName" /> <div class="text_left" id="nameValidMsg"> <p> 请填写有效的收件人姓名 </p> </div> </td> </tr> <tr> <td valign="top" class="w1"> 收件人详细地址: </td> <td> <input type="text" name="fullAddress" class="text_input" id="fullAddress" /> <div class="text_left" id="addressValidMsg"> <p> 请填写有效的收件人的详细地址 </p> </div> </td> </tr> <tr> <td valign="top" class="w1"> 邮政编码 </td> <td> <input type="text" class="text_input" name="postalCode" id="postalCode" /> <div class="text_left" id="codeValidMsg"> <p> 请填写有效的收件人的邮政编码 </p> </div> </td> </tr> <tr> <td valign="top" class="w1"> 电话 </td> <td> <input type="text" class="text_input" name="phone" id="phone" /> <div class="text_left" id="phoneValidMsg"> <p> 请填写有效的收件人的电话 </p> </div> </td> </tr> <tr> <td valign="top" class="w1"> 手机 </td> <td> <input type="text" class="text_input" name="mobile" id="mobile" /> <div class="text_left" id="mobileValidMsg"> <p> 请填写有效的收件人的手机 </p> </div> </td> </tr> </table> <div class="login_in"> <a href="order_info.jsp"><input id="btnClientRegister" class="button_1" name="submit" type="submit" value="取消" /></a> <input id="btnClientRegister" class="button_1" name="submit" type="submit" value="下一步" /> </div> </form> </div> <%@include file="../common/foot1.jsp"%> </body> </html>
chinoxl 2013-02-01
  • 打赏
  • 举报
回复
报错 null weresd null java.sql.SQLException: Column 'email' cannot be null at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2847) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531) at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1347) at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:958) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1957) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1880) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1741) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102) at org.tarena.dang.dao.JdbcUserDAO.save(JdbcUserDAO.java:33) at org.tarena.dang.action.user.RegisterAction.execute(RegisterAction.java:42) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441) at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243) at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252) at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:619)
AndyXuq 2013-02-01
  • 打赏
  • 举报
回复
楼上慧眼如炬啊..
aa11b111 2013-02-01
  • 打赏
  • 举报
回复
达内暴露无疑啊!!异常显示的是那么准确
chnaghong1990 2013-02-01
  • 打赏
  • 举报
回复
偶也遇到过 没仔细去看 猜想应该是在赋值的时候要先通过get去获取user对象吧 在给它赋值

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧