问题描述
最近做一个项目注册的时候,点提交后返回的总是struts2配置文件中input的result页面。代码如下<%@pagecontentType="text/html"pageEncoding="UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%><!DOCTYPEhtml><html><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"><title><s:textname="个人信息管理系统->注册"/></title></head><bodybgcolor="#CCCCFF"><s:formaction="registerAction"method="post"><tablealign="center"><tr><tdwidth="40%"><tableborder="2"bgcolor="#AABBCCDD"width="100%"align="center"><tr><tdcolspan="2"align="center"><fontcolor="yellow"><s:textname="请填写以下注册信息"/></font></td></tr><tr><td><s:textfieldname="loginname"label="登录名"/></td></tr><tr><td><s:passwordname="password1"label="密码"size="21"/></td></tr><tr><td><s:passwordname="password2"label="再次输入密码"size="21"/></td></tr><tr><td><s:textfieldname="name"label="用户真是姓名"/></td></tr><tr><td><s:textname="用户性别:"></s:text></td><td><inputtype="radio"name="sex"value="男"checked/>男<inputtype="radio"name="sex"value="女"/>女</td></tr><tr><td><s:textfieldname="birth"label="出生日期"/></td></tr><tr><td><s:textfieldname="nation"label="用户民族"/></td></tr><tr><td><s:selectname="edu"label="用户学历"headerValue="----------请选择----------"headerKey="1"list="{'硕士','博士','教授','本科','专科','高中','初中','小学','其它'}"></s:select></td></tr><tr><td><s:selectname="work"label="用户职称"headerValue="----------请选择----------"headerKey="1"list="{'软件测试工程师','软件开发工程师','教师','学生','职员','经理','老板','公务员','其它'}"></s:select></td></tr><tr><td><s:textfieldname="phone"label="用户电话"/></td></tr><tr><td><s:textfieldname="place"label="用户住址"/></td></tr><tr><td><s:textfieldname="email"label="用户邮箱"/></td></tr><tr><tdcolspan="2"align="center"><inputtype="submit"value="确定"/> <inputtype="reset"value="清空"/> <s:ahref="http://localhost:8084/ch04/login/index.jsp">返回</s:a></td></tr></table></td></tr></table></s:form></body></html>
解决方案
解决方案二:
下面是对应的ACTIONpackageedu.login.Action;importDBJavaBean.DB;importcom.opensymphony.xwork2.ActionSupport;importjava.sql.ResultSet;importjava.sql.SQLException;importjavax.servlet.http.HttpServletRequest;importjavax.swing.JOptionPane;importorg.apache.struts2.interceptor.ServletRequestAware;publicclassRegisterActionextendsActionSupportimplementsServletRequestAware{privateStringuserName;privateStringpassword1;privateStringpassword2;privateStringname;privateStringsex;privateStringbirth;privateStringnation;privateStringedu;privateStringwork;privateStringphone;privateStringplace;privateStringemail;privateResultSetrs=null;privateStringmessage="ERROR";privateHttpServletRequestrequest;publicStringgetUserName(){returnuserName;}publicvoidsetUserName(StringuserName){this.userName=userName;}publicStringgetPassword1(){returnpassword1;}publicvoidsetPassword1(Stringpassword1){this.password1=password1;}publicStringgetPassword2(){returnpassword2;}publicvoidsetPassword2(Stringpassword2){this.password2=password2;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetSex(){returnsex;}publicvoidsetSex(Stringsex){this.sex=sex;}publicStringgetBirth(){returnbirth;}publicvoidsetBirth(Stringbirth){this.birth=birth;}publicStringgetNation(){returnnation;}publicvoidsetNation(Stringnation){this.nation=nation;}publicStringgetEdu(){returnedu;}publicvoidsetEdu(Stringedu){this.edu=edu;}publicStringgetWork(){returnwork;}publicvoidsetWork(Stringwork){this.work=work;}publicStringgetPhone(){returnphone;}publicvoidsetPhone(Stringphone){this.phone=phone;}publicStringgetPlace(){returnplace;}publicvoidsetPlace(Stringplace){this.place=place;}publicStringgetEmail(){returnemail;}publicvoidsetEmail(Stringemail){this.email=email;}publicvoidsetServletRequest(HttpServletRequesthsr){request=hsr;}publicvoidmessage(Stringmsg){inttype=JOptionPane.YES_NO_OPTION;Stringtitle="信息提示";JOptionPane.showMessageDialog(null,msg,title,type);}@Overridepublicvoidvalidate(){if(getUserName()==null||getUserName().length()==0){addFieldError("userName","登录名字不允许为空!");}else{try{DBmysql=newDB();rs=mysql.selectMess(request,this.getUserName());if(rs.next()){addFieldError("userName","此登录名字已存在!");}}catch(SQLExceptionex){ex.printStackTrace();}}if(getPassword1()==null||getPassword1().length()==0){addFieldError("password1","登录密码不允许为空!");}if(getPassword2()==null||getPassword2().length()==0){addFieldError("password2","重复密码不允许为空!");}if(!(getPassword1().equals(getPassword2()))){addFieldError("password2","两次密码不一致!");}if(getName()==null||getName().length()==0){addFieldError("name","用户姓名不允许为空!");}if(getBirth()==null||getBirth().length()==0||getBirth().equals("yyyy-mm-dd")){addFieldError("birth","用户生日不允许为空!");}else{if(getBirth().length()!=10){addFieldError("birth","用户生日格式为'yyyy-mm-dd'!");}else{Stringan=this.getBirth().substring(4,5);Stringbn=this.getBirth().substring(7,8);if(!(an.equals("-"))||!(bn.equals("-"))){addFieldError("birth","用户生日格式为'yyyy-mm-dd'!");}}}if(getNation()==null||getNation().length()==0){addFieldError("nation","用户民族不允许为空!");}if(getEdu().equals("1")){addFieldError("edu","请选择用户学历!");}if(getWork().equals("1")){addFieldError("work","请选择用户工作!");}if(getPhone()==null||getPhone().length()==0){addFieldError("phone","用户电话不允许为空!");}if(getPlace()==null||getPlace().length()==0){addFieldError("place","用户地址不允许为空!");}if(getEmail()==null||getEmail().length()==0){addFieldError("email","用户email不允许为空!");}}publicStringexecute()throwsException{DBmysql=newDB();Stringmess=mysql.insertMess(request,this.getUserName(),this.getPassword1(),this.getName(),this.getSex(),this.getBirth(),this.getNation(),this.getEdu(),this.getWork(),this.getPhone(),this.getPlace(),this.getEmail());if(mess.equals("ok")){message="SUCCESS";}elseif(mess.equals("one")){message="input";}returnmessage;}}
解决方案三:
然后是最后一个ACTIONpackageDBJavaBean;importJavaBean.UserNameBean;importJavaBean.MyDayBean;importJavaBean.MyFileBean;importJavaBean.MyFriBean;importJavaBean.MyMessBean;importjava.sql.*;importjava.util.ArrayList;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpSession;importjavax.swing.JOptionPane;importorg.apache.struts2.interceptor.ServletRequestAware;//实现ServletRequestAware通过IoC方式直接访问Servlet,并通过request获取session对象publicclassDBimplementsServletRequestAware{privateStringdriverName="com.mysql.jdbc.Driver";privateStringurl="jdbc:mysql://localhost:3306/gbk?useUnicode=true&characterEncoding=gbk";privateStringuser="root";privateStringpassword="root";privateConnectioncon=null;privateStatementst=null;privateResultSetrs=null;privateHttpServletRequestrequest;publicDB(){}publicStringgetDriverName(){returndriverName;}publicvoidsetDriverName(StringdriverName){this.driverName=driverName;}publicStringgetUrl(){returnurl;}publicvoidsetUrl(Stringurl){this.url=url;}publicStringgetUser(){returnuser;}publicvoidsetUser(Stringuser){this.user=user;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicvoidsetServletRequest(HttpServletRequesthsr){request=hsr;}//完成连接数据库操作,并生成容器返回publicStatementgetStatement(){try{Class.forName(getDriverName());con=DriverManager.getConnection(getUrl(),getUser(),getPassword());returncon.createStatement();}catch(Exceptione){e.printStackTrace();returnnull;}}//完成注册,把用户的注册信息录入到数据库中publicStringinsertMess(HttpServletRequestrequest,StringuserName,Stringpassword,Stringname,Stringsex,Stringbirth,Stringnation,Stringedu,Stringwork,Stringphone,Stringplace,Stringemail){try{Stringsure=null;rs=selectMess(request,userName);//判断是否用户名已存在,如果存在返回oneif(rs.next()){sure="one";}else{Stringsql="insertintouser"+"(userName,password,name,sex,birth,nation,edu,work,phone,place,email)"+"values("+"'"+userName+"'"+","+"'"+password+"'"+","+"'"+name+"'"+","+"'"+sex+"'"+","+"'"+birth+"'"+","+"'"+nation+"'"+","+"'"+edu+"'"+","+"'"+work+"'"+","+"'"+phone+"'"+","+"'"+place+"'"+","+"'"+email+"'"+")";st=getStatement();introw=st.executeUpdate(sql);if(row==1){//调用,myMessage方法,更新session中保存的用户信息Stringmess=myMessage(request,userName);if(mess.equals("ok")){sure="ok";}else{sure=null;}}else{sure=null;}}returnsure;}catch(Exceptione){e.printStackTrace();returnnull;}}//更新注册的个人信息publicStringupdateMess(HttpServletRequestrequest,StringuserName,Stringname,Stringsex,Stringbirth,Stringnation,Stringedu,Stringwork,Stringphone,Stringplace,Stringemail){try{Stringsure=null;Stringsql="updateusersetname='"+name+"',sex='"+sex+"',birth='"+birth+"',nation='"+nation+"',edu='"+edu+"',work='"+work+"',phone='"+phone+"',place='"+place+"',email='"+email+"'whereuserName='"+userName+"'";st=getStatement();introw=st.executeUpdate(sql);if(row==1){//调用,myMessage方法,更新session中保存的用户信息Stringmess=myMessage(request,userName);if(mess.equals("ok")){sure="ok";}else{sure=null;}}else{sure=null;}returnsure;}catch(Exceptione){e.printStackTrace();returnnull;}}//查询个人信息,并返回rspublicResultSetselectMess(HttpServletRequestrequest,StringuserName){try{Stringsql="select*fromuserwhereuserName='"+userName+"'";st=getStatement();returnst.executeQuery(sql);}catch(Exceptione){e.printStackTrace();returnnull;}}//把个人信息通过MyMessBean,保存到session对象中publicStringmyMessage(HttpServletRequestrequest,StringuserName){try{ArrayListlistName=null;HttpSessionsession=request.getSession();listName=newArrayList();rs=selectMess(request,userName);while(rs.next()){MyMessBeanmess=newMyMessBean();mess.setName(rs.getString("name"));mess.setSex(rs.getString("sex"));mess.setBirth(rs.getString("birth"));mess.setNation(rs.getString("nation"));mess.setEdu(rs.getString("edu"));mess.setWork(rs.getString("work"));mess.setPhone(rs.getString("phone"));mess.setPlace(rs.getString("place"));mess.setEmail(rs.getString("email"));listName.add(mess);session.setAttribute("MyMess",listName);}return"ok";}catch(Exceptione){e.printStackTrace();returnnull;}}
解决方案四:
然后是struts2的配置文件<!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><includefile="example.xml"/><!--Configurationforthedefaultpackage.--><packagename="default"extends="struts-default"><actionname="loginAction"class="edu.login.Action.LoginAction"><resultname="SUCCESS">/mainFrame/main.jsp</result><resultname="input">/login/index.jsp</result><resultname="ERROR">/login/index.jsp</result></action><actionname="registerAction"class="edu.login.Action.RegisterAction"><resultname="SUCCESS">/login/index.jsp</result><resultname="input">/login/register.jsp</result><resultname="ERROR">/login/register.jsp</result></action><actionname="upMessAction"class="edu.personManager.Action.UpdateMessAction"><resultname="SUCCESS">/personMessage/lookMessage.jsp</result><resultname="input">/personMessage/updateMessage.jsp</result><resultname="ERROR">/personMessage/updateMessage.jsp</result></action><actionname="upPassAction"class="edu.personManager.Action.UpdatePassAction"><resultname="SUCCESS">/personMessage/lookMessage.jsp</result><resultname="input">/personMessage/updatePass.jsp</result></action><actionname="addFriAction"class="edu.friendManager.Action.AddFriAction"><resultname="SUCCESS">/friendManager/lookFriends.jsp</result><resultname="input">/friendManager/addFriend.jsp</result></action><actionname="findFriAction"class="edu.friendManager.Action.FindFriAction"><resultname="SUCCESS">/friendManager/findFriend.jsp</result><resultname="ERROR">/friendManager/lookFriends.jsp</result><resultname="input">/friendManager/lookFriends.jsp</result></action><actionname="upFriAction"class="edu.friendManager.Action.UpdateFriAction"><resultname="SUCCESS">/friendManager/lookFriends.jsp</result><resultname="input">/friendManager/updateFriend.jsp</result></action><actionname="deleteFriAction"class="edu.friendManager.Action.DeleteFriAction"><resultname="SUCCESS">/friendManager/lookFriends.jsp</result></action><actionname="addDayAction"class="edu.dateTimeManager.Action.AddDayAction"><resultname="SUCCESS">/dateTimeManager/lookDay.jsp</result><resultname="input">/dateTimeManager/addDay.jsp</result><resultname="ERROR">/dateTimeManager/addDay.jsp</result></action><actionname="findDayAction"class="edu.dateTimeManager.Action.FindDayAction"><resultname="SUCCESS">/dateTimeManager/findDay.jsp</result><resultname="input">/dateTimeManager/lookDay.jsp</result><resultname="ERROR">/dateTimeManager/lookDay.jsp</result></action><actionname="upDayAction"class="edu.dateTimeManager.Action.UpdateDayAction"><resultname="SUCCESS">/dateTimeManager/lookDay.jsp</result><resultname="input">/dateTimeManager/updateDay.jsp</result><resultname="ERROR">/dateTimeManager/updateDay.jsp</result></action><actionname="deleteDayAction"class="edu.dateTimeManager.Action.DeleteDayAction"><resultname="SUCCESS">/dateTimeManager/lookDay.jsp</result></action><actionname="addFileAction"class="edu.fileManager.Action.AddFileAction"><interceptor-refname="fileUpload"><paramname="maximumSize">1024000000</param></interceptor-ref><interceptor-refname="defaultStack"/><paramname="savePath">/save</param><resultname="SUCCESS">/fileManager/success.jsp</result><resultname="input">/fileManager/fileUp.jsp</result><resultname="ERROR">/fileManager/fileUp.jsp</result></action><actionname="findFileAction"class="edu.fileManager.Action.FindFileAction"><resultname="SUCCESS">/fileManager/findFile.jsp</result><resultname="input">/fileManager/lookFile.jsp</result><resultname="ERROR">/fileManager/lookFile.jsp</result></action><actionname="deleteFileAction"class="edu.fileManager.Action.DeleteFileAction"><resultname="SUCCESS">/fileManager/lookFile.jsp</result><resultname="ERROR">/fileManager/findFile.jsp</result></action><actionname="downFileAction"class="edu.fileManager.Action.DownFileAction"><paramname="path">/save/${downloadFileName}</param><resultname="SUCCESS"type="stream"><paramname="contentType">application/octet-stream;charset=ISO8859-1</param><paramname="inputName">InputStream</param><paramname="contentDisposition">attachment;filename="${downloadFileName}"</param><paramname="bufferSize">40960</param></result></action></package></struts>
解决方案五:
这么花很难看,你可以设置一个断点在action的validate方法和execute方法的第一行,然后单步跟踪下去,你就会很清晰的看见为何返回input,如果没有进入相应方法,你就要考虑在拦截器设置断点了,目测应该是mysql.insertMess方法失败了,你可以单步跟踪进入这个方法,
解决方案六:
必须错,Stringsql="insertintouser"+"(userName,password,name,sex,birth,nation,edu,work,phone,place,email)"+"values("+"'"+userName+"'"+","+"'"+password+"'"+","+"'"+name+"'"+","+"'"+sex+"'"+","+"'"+birth+"'"+","+"'"+nation+"'"+","+"'"+edu+"'"+","+"'"+work+"'"+","+"'"+phone+"'"+","+"'"+place+"'"+","+"'"+email+"'"+")";这句sql语句肯定有问题。看了下,每个参数都多一对单引号。
解决方案七:
写sql语句要这么写吗,出个错,位置都找不到
解决方案八:
自己单步调试一下吧,看看代码是如何运行的?
解决方案九:
你的request用对了吗
解决方案十:
如果你没定义input却爆了带input的异常可能是表单验证的问题你注解掉action的方法不进行表单验证就行
解决方案十一:
引用5楼fangmingshijie的回复:
必须错,Stringsql="insertintouser"+"(userName,password,name,sex,birth,nation,edu,work,phone,place,email)"+"values("+"'"+userName+"'"+","+"'"+password+"'"+","+"'"+name+"'"+","+"'"+sex+"'"+","+"'"+birth+"'"+","+"'"+nation+"'"+","+"'"+edu+"'"+","+"'"+work+"'"+","+"'"+phone+"'"+","+"'"+place+"'"+","+"'"+email+"'"+")";这句sql语句肯定有问题。看了下,每个参数都多一对单引号。
把单引号删了但还是那个问题返回的依然是input。我是新手多谢帮助。
解决方案十二:
看了头晕总结:稀烂
解决方案十三:
回复于:2013-05-2211:01:43如果你没定义input却爆了带input的异常可能是表单验证的问题你注解掉action的方法不进行表单验证就行
解决方案十四:
你可以通过输出语句去查看下,执行到哪里给返回了。或者用debug,打上单点去进行调试。最好还是用debug去调试下吧!看看是那块的问题。
解决方案十五:
这是你Action中的方法中的问题。
解决方案:
做个注册登录有那么困难啊?好好学习ssh吧
解决方案:
哎哟。。你第二次注册的时候查询肯定有了。。当然返回input页面。//判断是否用户名已存在,如果存在返回oneif(rs.next()){sure="one";}
解决方案:
你注册页面登录名name=“loginname”数据处理页面又定义userName明显不匹配把register.jsp登录名name="userName"就ok了
解决方案:
注意你的那个“出生日期”的提交格式,这个错了就会报这种错误。