wm8466
2008-11-13 12:11
浏览 294
已采纳

struts2 页面传值

正在学习struts2 遇到了一个头疼的问题 就是在做信息修改的时候一直无法传值到 action

jsp页面代码:account_address.jsp

<s:form action="regupdate.action" method="post" theme="simple">

 
     


       


       


     


       


       


       


     


       


       


       


     


       


       


     


     


       


       


     


     


       


       


     


     


       


       


     


     


       


       


     


     


       


       


     


     


       


       


   


联系信息:
姓名:

        <s:textfield name="userid" value="%{user.userid}"> </s:textfield>

        <s:hidden name="user.realname" value="%{user.realname.trim()}"> </s:hidden>

        <s:hidden name="user.email" value="%{user.email.trim()}"> </s:hidden>

        <s:hidden name="user.email" value="%{user.email.trim()}"> </s:hidden>

        <s:textfield name="user.infoname" id="name1" value="%{user.infoname.trim()}"> </s:textfield>

        <s:fielderror theme="simple">

        <s:param>user.infoname </s:param>

        </s:fielderror>

        <s:fielderror theme="simple">

        <s:param>user.email </s:param>

        </s:fielderror>

        <s:fielderror theme="simple">

        <s:param>user.realname </s:param>

        </s:fielderror>

       
联系人:

        <s:textfield name="user.contact" id="contact" value="%{user.contact.trim()}"> </s:textfield>

       
性别:

        <s:radio list="#{0:'男',1:'女'}" name="user.sex" id="sex" listkey="key" listvalue="value" value="%{user.sex}"> </s:radio>

     

       
地区:

    <s:textfield name="user.area" id="area" value="%{user.area.trim()}"> </s:textfield>

联系地址:

<s:textfield name="user.address" id="address" value="%{user.address.trim()}"> </s:textfield>

<s:fielderror>

<s:param>user.address </s:param>

</s:fielderror>

邮编:

<s:textfield name="user.zipcode" id="zipcode" value="%{user.zipcode.trim()}"> </s:textfield>

联系电话:

        <s:textfield name="user.phone" id="phone" value="%{user.phone.trim()}"> </s:textfield>

       
传真:

        <s:textfield name="user.fax" id="fax" value="%{user.fax.trim()}"> </s:textfield>

       


        <input name="but1" class="but1" type="button" value="">

       


   


   
     


       


     


     


       


       


     


     


       


       


     


     


       


       


     


     


       


       


     


     


       


       


     


     


       


       


     


     


       


       


     


     


       


       


     


     


       


     


   
送货信息:
姓名:

        <s:textfield name="user.goname" id="name2" value="%{user.goname.trim()}"> </s:textfield> 

        <s:fielderror>

<s:param>user.goname </s:param>

</s:fielderror>

       
联系人:

        <s:textfield name="user.contact1" id="contact1" value="%{user.contact1.trim()}"> </s:textfield>

       
性别:

        <s:radio list="#{0:'男',1:'女'}" name="user.sex1" id="sex1" listkey="key" listvalue="value" value="%{user.sex1}"> </s:radio>

       
地区:

        <s:textfield name="user.area1" id="area1" value="%{user.area1}"> </s:textfield>

       
送货地址:

        <s:textfield name="user.address1" id="address1" value="%{user.address1}"> </s:textfield>

        <s:fielderror>

<s:param>user.address1 </s:param>

</s:fielderror>

       
?邮编: <s:textfield name="user.zipcode1" id="zipcode1" value="%{user.zipcode1.trim()}"> </s:textfield>
联系电话:

<s:textfield name="user.phone1" id="phone1" value="%{user.phone1.trim()}"> </s:textfield>

传真:

<s:textfield name="user.fax1" id="fax1" value="%{user.fax1.trim()}"> </s:textfield>



        <s:submit cssclass="subbut" value="" name="subbtn1"> </s:submit>

       


    </s:form>

java代码:RegisterAction.java

public class RegisterAction extends ActionSupport{

private Users user;

private String password;

private String confirm;

private String mess;

public String view() throws Exception

{

Map session = ActionContext.getContext().getSession();

Users u = (Users)session.get("unique");

UserDAO userdao = new UserDAO();

user = userdao.get(u.getEmail());

return SUCCESS;

}

public String execute() throws Exception

{



MD5 md5 = new MD5();

UserDAO userdao = new UserDAO();

if(!userdao.find(user.getEmail()))

{

user.setPassword(md5.getMD5ofStr(password));

UserDAO userdao2 = new UserDAO();

userdao2.create(user); // 如果用户不存在,那么创建用户

Map sess = ActionContext.getContext().getSession();

sess.put("unique", user); //将用户状态置为登陆

System.out.println("Registered Successfully"); //控制台打印信息

return SUCCESS;

}

addFieldError("user.username","会员"+user.getEmail()+"已经存在");

return ERROR;

}



public String update() throws Exception {

    Map session=ActionContext.getContext().getSession();

Users u=(Users)session.get("unique");

UserDAO userdao=new UserDAO();

System.out.println(u.getEmail());

Users user1 = userdao.get(u.getEmail());

int id = user.getUserid();

user1 = user;

user1.setUserid(id);

userdao.update(user);

mess="User Info Updated Successfully";

return SUCCESS;

}







public String getPassword() {

return password;

}



public void setPassword(String password) {

this.password = password;

}



public Users getUser() {

return user;

}



public void setUser(Users user) {

this.user = user;

}



public String getConfirm() {

return confirm;

}



public void setConfirm(String confirm) {

this.confirm = confirm;

}



public String getMess() {

return mess;

}



public void setMess(String mess) {

this.mess = mess;

}

}

struts.xml

struts.xml 代码  



<package name="users" extends="struts-default" namespace="/user">

 



<action name="reg_*" class="com.carshop.action.RegisterAction" method="{1}">

<interceptor-ref name="logger"> </interceptor-ref>

  <result name="success">/user/account_address.jsp </result>

  <result name="input">/reg.jsp </result>

  </action>

 



<action name="regupdate" class="com.carshop.action.RegisterAction" method="update">  

<interceptor-ref name="params">  

<result name="success">/include/info.jsp </result>

<result name="input" type="chain">reg_input </result>

<result name="error">/include/error.jsp </result>

</action>

异常:

exception



javax.servlet.ServletException

org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)

org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)

org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)





root cause



java.lang.NullPointerException

com.carshop.action.RegisterAction.update(RegisterAction.java:73)

sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

java.lang.reflect.Method.invoke(Unknown Source)

com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)

com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)

com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:167)

com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)

com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)

com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)

com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)

org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)

org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)

org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)

org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • catstiger 2008-11-13 13:15
    已采纳

    user是空的,你应该private User user=new User();
    更地道的做法是实现ModelDriven接口,其中的:
    getModel()方法返回user:
    public Object getModel() {return model;}
    页面上的:或者都可以。
    需要一个ModelDriverInterceptor,这个在struts-default中已经有了。

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • iteye_14081 2008-11-13 12:15

    是这个的问题:
    [code="java"]UserDAO userdao = new UserDAO(); [/code]

    这个应该注入,而不应该new出来。

    评论
    解决 无用
    打赏 举报
  • iteye_521 2008-11-13 12:32

    [quote]update()[/quote]
    这个方法中设置一个断点DEBUG一下不就知道了吗,空指针问题,很常见的

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题