问题场景: 在使用表单向Action传递数据的时候, 遇到了这个问题, 导致了空指针异常.

问题描述:

  1. 10:14:56.622 [http-nio-8080-exec-45] ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor - Developer Notification (set struts.devMode to false to disable this message):
  2. Unexpected Exception caught setting 'password' on 'class com.bj186.crm.web.action.UserAction: Error setting expression 'password' with value ['123456', ]
  3. 10:14:56.648 [http-nio-8080-exec-45] ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor - Developer Notification (set struts.devMode to false to disable this message):
  4. Unexpected Exception caught setting 'submit' on 'class com.bj186.crm.web.action.UserAction: Error setting expression 'submit' with value ['', ]
  5. 10:14:56.649 [http-nio-8080-exec-45] ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor - Developer Notification (set struts.devMode to false to disable this message):
  6. Unexpected Exception caught setting 'username' on 'class com.bj186.crm.web.action.UserAction: Error setting expression 'username' with value ['艾格尼丝', ]
  7. java.lang.NullPointerException
  8. at com.bj186.crm.web.action.UserAction.login(UserAction.java:70)
  9. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  10. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  11. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  12. at java.lang.reflect.Method.invoke(Method.java:498)
  13. at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:902)
  14. at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1547)
  15. at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)

问题分析: 这个问题非常隐蔽, 是Struts2在接受值的时候, 不能正确的接收. 问题的根本原因在于没有创建接收数据的对象

解决思路: 在Action中把需要接收数据的类的对象new出来

  1. private User user = new User();

并且设置它的getter和setter方法

然后这个问题就解决了!!!

附Struts2的UserAction.java的代码

  1. package com.bj186.crm.web.action;
  2.  
  3. import com.bj186.crm.entity.User;
  4. import com.bj186.crm.service.UserService;
  5. import com.opensymphony.xwork2.Action;
  6. import com.opensymphony.xwork2.ActionContext;
  7. import com.opensymphony.xwork2.ActionSupport;
  8. import com.opensymphony.xwork2.ModelDriven;
  9. import com.opensymphony.xwork2.util.ValueStack;
  10. import org.apache.struts2.ServletActionContext;
  11. import org.springframework.web.context.WebApplicationContext;
  12. import org.springframework.web.context.support.WebApplicationContextUtils;
  13.  
  14. import javax.servlet.ServletContext;
  15. import java.util.Arrays;
  16. import java.util.List;
  17.  
  18. public class UserAction extends ActionSupport implements ModelDriven<User> {
  19. private User user = new User();
  20. private UserService userService;
  21. private ValueStack valueStack;
  22.  
  23. //测试添加上User的get和set方法
  24. public void setUser(User user) {
  25. //ServletActionContext.getRequest().getParameter("username")
  26. this.user = user;
  27. }
  28.  
  29. public User getUser() {
  30. return user;
  31. }
  32.  
  33. public void setUserService(UserService userService) {
  34. this.userService = userService;
  35. }
  36.  
  37. public UserAction() {
  38. //1.获取ServletContext
  39. ServletContext servletContext = ServletActionContext.getServletContext();
  40. //2.通过整合包中的工具类, 从servletContext的作用域上获取目标--Spring的容器WebApplicationContext
  41. WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
  42. //3.从Spring的容器中获取对象
  43. userService = webApplicationContext.getBean("userService", UserService.class);
  44. System.out.println("userService:"+userService);
  45. //4.获取值栈
  46. ActionContext context = ActionContext.getContext();
  47. valueStack = context.getValueStack();
  48. }
  49.  
  50. public String execute() {
  51. System.out.println("正在UserAction的execute()方法中!");
  52. return Action.SUCCESS;
  53. }
  54.  
  55. /**
  56. * 添加用户
  57. * @return 字符串状态
  58. */
  59. public String register() {
  60. User user = (User) valueStack.findValue("user");
  61. userService.register(user);
  62. return Action.SUCCESS;
  63. }
  64.  
  65. /**
  66. * 用户登录
  67. * @return
  68. */
  69. public String login() {
  70. String username = user.getUsername();
  71. String password = user.getPassword();
  72. boolean isLogin = userService.verifyLogin(username,password);
  73. return isLogin? Action.SUCCESS: Action.NONE;
  74. }
  75.  
  76. /**
  77. * 删除用户
  78. * @return 字符串状态
  79. */
  80. public String deleteUser(Integer uid) {
  81. userService.deleteUser(uid);
  82. return Action.SUCCESS;
  83. }
  84.  
  85. /**
  86. * 修改用户
  87. * @return 字符串状态
  88. */
  89. public String updateUser() {
  90. User user = (User)valueStack.findValue("user");
  91. userService.updateUser(user);
  92. return Action.SUCCESS;
  93. }
  94.  
  95. /**
  96. * 查询用户
  97. * @return 字符串状态
  98. */
  99. public String getUserById(Integer uid) {
  100. User user = userService.selectUserById(uid);
  101. System.out.println("查询出来的用户是: " + user);
  102. return Action.SUCCESS;
  103. }
  104.  
  105. /**
  106. * 显示用户页面
  107. * @return
  108. */
  109. public String showAllUsers() {
  110. System.out.println("显示所有的用户");
  111. return Action.SUCCESS;
  112. }
  113.  
  114. /**
  115. * 查询出所有的用户
  116. * @return
  117. */
  118. public String selectAllUsers() {
  119. List<User> userList = userService.selectAllUsers();
  120. valueStack.set("userList",userList);
  121. System.out.println("查到的用户个数为: " + userList.size());
  122. return Action.SUCCESS;
  123. }
  124.  
  125. @Override
  126. public User getModel() {
  127. return user;
  128. }
  129. }

前端页面的index.jsp代码

  1. <%-- Created by IntelliJ IDEA. --%>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>用户管理系统首页</title>
  6. </head>
  7. <body>
  8. <h2 style="color:red;">欢迎光临!</h2>
  9. <form action="<%=request.getContextPath()%>/user_login.action" method="post">
  10. <input name="username" type="text"><br/>
  11. <input name="password" type="password"><br/>
  12. <button name="submit" type="submit">登录</button>
  13. </form>
  14. </body>
  15. </html>

Unexpected Exception caught setting 'username' on 'class com.bj186.crm.web.action.UserAction: Error setting expression 'username' with value ['艾格尼丝', ]的更多相关文章

  1. hibernate 异常:Unexpected Exception caught setting

    异常信息:Unexpected Exception caught setting 'outHeight' on 'class com.srpm.core.project.seismicFortific ...

  2. 五)使用 easyui-tabs 遭遇错误 Unexpected Exception caught setting '_' on

    十月 10, 2015 3:08:35 下午 com.opensymphony.xwork2.interceptor.ParametersInterceptor error 严重: Developer ...

  3. Unexpected exception 'Cannot run program ... error=2, No such file or directory' ... adb'

    Eclipse ADT Unexpected exception 'Cannot run program' up vote 8 down vote favorite 4 I have installe ...

  4. myEclipse Could not create the view: An unexpected exception was thrown.

    myEclipse 非正常关闭,打开后 service Explorer or Package Explorer 视图显示不出来.报“Could not create the view: An une ...

  5. (转)Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误

    问题:电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected excep ...

  6. Could not create the view: An unexpected exception was thrown 异常处理

    MyEclipse 打开后有时候莫名的在server窗口里抛出"Could not create the view: An unexpected exception was thrown&q ...

  7. Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误

    电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected exceptio ...

  8. Could not create the view: An unexpected exception was thrown.问题解决

    Could not create the view: An unexpected exception was thrown.问题解决 今天打开Myeclipse10的时候,发现server窗口出现一堆 ...

  9. An unexpected exception occurred while creating a change object. see the error log for more details

    今天再给Android项目工程中的包重命名时出现了这个错误(之前重命名的时候就没有出现,郁闷): An unexpected exception occurred while creating a c ...

随机推荐

  1. org.hibernate.hql.ast.QuerySyntaxException: Student is not mapped [from Student as stu where stu.sclass=?]

    java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: t_aty_disease is not ...

  2. 【211】win10快捷键大全

    参考:win10快捷键大全 win10常用快捷键 • 贴靠窗口:Win +左/右> Win +上/下>窗口可以变为1/4大小放置在屏幕4个角落 • 切换窗口:Alt + Tab(不是新的, ...

  3. 洛谷 P2296 寻找道路【bfs+spfa】

    反向建边bfs出不能到t的点,然后对每个能到这些点的点打上del标记,然后spfa的时候不经过这些点即可 #include<iostream> #include<cstdio> ...

  4. thinkphp5 +elasticsearch

    php7使用elasticsearch 1.安装 官网下载地址:https://www.elastic.co/downloads/elasticsearch # 解压到非root目录,运行时使用非ro ...

  5. JSP | 基础 | JSP行为 | incline && forward

    语法 描述 jsp:include 用于在当前页面中包含静态或动态资源 jsp:forward 从一个JSP文件向另一个文件传递一个包含用户请求的request对象 index.jsp <%@ ...

  6. FZu Problem 2236 第十四个目标 (线段树 + dp)

    题目链接: FZu  Problem 2236 第十四个目标 题目描述: 给出一个n个数的序列,问这个序列内严格递增序列有多少个?不要求连续 解题思路: 又遇到了用线段树来优化dp的题目,线段树节点里 ...

  7. LCA+树状数组 POJ 2763 Housewife Wind

    题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储 ...

  8. python tkinter窗口弹出置顶的方法

    加上下面两句即可实现root窗口的置顶显示,可以用于某些程序的消息提示,能够弹出到桌面显示 root = Tk() root.wm_attributes('-topmost',1)

  9. 在虚拟机里安装windows或Linux系统时,安装窗口过大按钮有时点不到解决办法(图文详解)

    不多说,直接上干货! 问题详情 解决办法 很简单快捷的解决办法,就是快捷键ALT+F7,可以拖动窗口的位置. 成功!

  10. 通俗易懂的Nhibernate教程(2) ---- 配置之Nhibernate配置

    在上一个教程中,我们讲了Nhibernate的基本使用!So,让我们回顾下Nhibernate使用基本的步骤吧 1.NHibernate配置  ----- 这一步我们告诉了Nhibernate:数据库 ...