Unexpected Exception caught setting 'username' on 'class com.bj186.crm.web.action.UserAction: Error setting expression 'username' with value ['艾格尼丝', ]
问题场景: 在使用表单向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>
Unexpected Exception caught setting 'username' on 'class com.bj186.crm.web.action.UserAction: Error setting expression 'username' with value ['艾格尼丝', ]的更多相关文章
- hibernate 异常:Unexpected Exception caught setting
异常信息:Unexpected Exception caught setting 'outHeight' on 'class com.srpm.core.project.seismicFortific ...
- 五)使用 easyui-tabs 遭遇错误 Unexpected Exception caught setting '_' on
十月 10, 2015 3:08:35 下午 com.opensymphony.xwork2.interceptor.ParametersInterceptor error 严重: Developer ...
- 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 ...
- myEclipse Could not create the view: An unexpected exception was thrown.
myEclipse 非正常关闭,打开后 service Explorer or Package Explorer 视图显示不出来.报“Could not create the view: An une ...
- (转)Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误
问题:电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected excep ...
- Could not create the view: An unexpected exception was thrown 异常处理
MyEclipse 打开后有时候莫名的在server窗口里抛出"Could not create the view: An unexpected exception was thrown&q ...
- Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误
电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected exceptio ...
- Could not create the view: An unexpected exception was thrown.问题解决
Could not create the view: An unexpected exception was thrown.问题解决 今天打开Myeclipse10的时候,发现server窗口出现一堆 ...
- 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 ...
随机推荐
- windows下安装RabbitMQ消息服务器 + 读写队列
RabbitMQ是什么 ? RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. 1:安装RabbitMQ需要先安装Erla ...
- hibernate -- 分页模糊查询中setParameter 和setParameterList
在分页模糊查询中碰到setParameter 和setParameterList这两个方法 setParameter 以前就只会用setParameter(int arg,String str),我用 ...
- CodeForces - 504A && CodeForces - 624C && CodeForces - 2B
Points 1. 关键要看到以度数为1的点作为突破口. 2. 关键是发现两者不同只能是a-c,而剩余的点必须为b 3. 注意0的情况.
- Redis的相关命令
Redis的相关命令 redis程序的命令 /usr/bin/redis-benchmark /usr/bin/redis-check-aof /usr/bin/redis-check-rdb /us ...
- 【废弃】JavaScript 控制类语法
创建: 2017/10/07 完成: 2017/10/07 结束: 2019/02/19 把所有内容迁出到[JavaScript 基础]并将本博文归档到[废弃] return前加一个空格, 使所有单元 ...
- BZOJ2217:Lollipop
题意 给定一个由1和2组成的序列,多次询问是否存在一个区间满足区间和=x 分析 结论:假如存在一个子串和为x,那么一定存在一个前缀,和为x或x+1 证明:可以认为原串是由和为x的串在开头和结尾添加若干 ...
- 2016 Multi-University Training Contest 1 GCD【RMQ+二分】
因为那时候没怎么补所以就分到了未搞分组里!!!然后因为标题如此之屌吧= =点击量很高,然后写的是无思路,23333,估计看题人真的是觉得博主就是个撒缺.废话不多说了,补题... update////2 ...
- bzoj 1433: [ZJOI2009]假期的宿舍【匈牙利算法】
i能睡j床的连边(i,j),跑最大匹配或者最大流,然后看看人数能不能对上总数即可 #include<iostream> #include<cstdio> #include< ...
- pycharm命令行快捷启动
打开 本用户目录下的.bashrc文件 vim .bashrc 在末尾添加一行 alias pycharm="the-path-to-pycharm.sh" 最后保存退出 然后更新 ...
- memcached原理及介绍
memcached是一种缓存技术,在存储在内存中(高性能分布式内存缓存服务器).目的 : 提速.(传统的都是把数据保存在关系型数据库管理系统既RDBMS,客户端请求时会从RDBS中读取数据并在浏览器中 ...