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

问题描述:

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):
Unexpected Exception caught setting 'password' on 'class com.bj186.crm.web.action.UserAction: Error setting expression 'password' with value ['123456', ]
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):
Unexpected Exception caught setting 'submit' on 'class com.bj186.crm.web.action.UserAction: Error setting expression 'submit' with value ['', ]
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):
Unexpected Exception caught setting 'username' on 'class com.bj186.crm.web.action.UserAction: Error setting expression 'username' with value ['艾格尼丝', ]
java.lang.NullPointerException
at com.bj186.crm.web.action.UserAction.login(UserAction.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:902)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1547)
at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)

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

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

private User user = new User();

并且设置它的getter和setter方法

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

附Struts2的UserAction.java的代码

package com.bj186.crm.web.action;

import com.bj186.crm.entity.User;
import com.bj186.crm.service.UserService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.ServletContext;
import java.util.Arrays;
import java.util.List; public class UserAction extends ActionSupport implements ModelDriven<User> {
private User user = new User();
private UserService userService;
private ValueStack valueStack; //测试添加上User的get和set方法
public void setUser(User user) {
//ServletActionContext.getRequest().getParameter("username")
this.user = user;
} public User getUser() {
return user;
} public void setUserService(UserService userService) {
this.userService = userService;
} public UserAction() {
//1.获取ServletContext
ServletContext servletContext = ServletActionContext.getServletContext();
//2.通过整合包中的工具类, 从servletContext的作用域上获取目标--Spring的容器WebApplicationContext
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//3.从Spring的容器中获取对象
userService = webApplicationContext.getBean("userService", UserService.class);
System.out.println("userService:"+userService);
//4.获取值栈
ActionContext context = ActionContext.getContext();
valueStack = context.getValueStack();
} public String execute() {
System.out.println("正在UserAction的execute()方法中!");
return Action.SUCCESS;
} /**
* 添加用户
* @return 字符串状态
*/
public String register() {
User user = (User) valueStack.findValue("user");
userService.register(user);
return Action.SUCCESS;
} /**
* 用户登录
* @return
*/
public String login() {
String username = user.getUsername();
String password = user.getPassword();
boolean isLogin = userService.verifyLogin(username,password);
return isLogin? Action.SUCCESS: Action.NONE;
} /**
* 删除用户
* @return 字符串状态
*/
public String deleteUser(Integer uid) {
userService.deleteUser(uid);
return Action.SUCCESS;
} /**
* 修改用户
* @return 字符串状态
*/
public String updateUser() {
User user = (User)valueStack.findValue("user");
userService.updateUser(user);
return Action.SUCCESS;
} /**
* 查询用户
* @return 字符串状态
*/
public String getUserById(Integer uid) {
User user = userService.selectUserById(uid);
System.out.println("查询出来的用户是: " + user);
return Action.SUCCESS;
} /**
* 显示用户页面
* @return
*/
public String showAllUsers() {
System.out.println("显示所有的用户");
return Action.SUCCESS;
} /**
* 查询出所有的用户
* @return
*/
public String selectAllUsers() {
List<User> userList = userService.selectAllUsers();
valueStack.set("userList",userList);
System.out.println("查到的用户个数为: " + userList.size());
return Action.SUCCESS;
} @Override
public User getModel() {
return user;
}
}

前端页面的index.jsp代码

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

最新文章

  1. Centos 6.X基本维护操作
  2. Java单例模式——并非看起来那么简单
  3. Redis学习笔记五:独立功能之事务
  4. JVM内存结构之二--新生代及新生代里的两个Survivor区(下一轮S0与S1交换角色,如此循环往复)、常见调优参数
  5. C中调用Lua函数
  6. iOS 完美解决 interactivePopGestureRecognizer 卡住的问题
  7. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) 几道简单题的题解
  8. rsync命令(同步/备份数据)
  9. Java笔记(十九)&hellip;&hellip;多线程
  10. Python: 设计模式 之 工厂模式例(1)
  11. Spring IOC的配置使用(转)
  12. HelloWorld——Cocos2d-x学习历程(二)
  13. UVa 10131: Is Bigger Smarter?
  14. Wpf中鼠标样式的修改,作用点修改
  15. 批量安装python库函数---pip
  16. backtrack 使用Tab键补全命令
  17. 跳槽 &amp; 思维导图
  18. npm -D -S 的区别
  19. Chimee - 简单易用的H5视频播放器解决方案
  20. docker构建本地仓库后,无法登陆解决办法(CentOS/Ubuntu)

热门文章

  1. 2.27 MapReduce Shuffle过程如何在Job中进行设置
  2. jqGrid 编辑完数据后能返回到当前位置的方法
  3. Android Handler消息机制源码解析
  4. poj2893 M*N puzzle 【n*m数码问题小结】By cellur925
  5. hdu 3484 Interviewe RMQ+二分
  6. css新奇技术及其未来发展
  7. 493 Reverse Pairs 翻转对
  8. [已读]JavaScript编程精解
  9. 【C#】枚举
  10. vs 2017 下 千万不要装force utf8 这个插件