gpt4 book ai didi

java - struts2中使用拦截器时出现NullPointerException

转载 作者:行者123 更新时间:2023-12-01 11:15:37 25 4
gpt4 key购买 nike

我的问题是,当我使用 Struts2 和 hibernate 登录到我的 web 应用程序时,出现空指针异常我的tomcat服务器出现异常如下:

INFOS: Server startup in 10968 ms
inside execute
inside find
java.lang.NullPointerException
Nameeee admin passwordddd admin
session getuser null
at com.inwi.dao.UserDAOImpl.find(UserDAOImpl.java:43)
at com.inwi.action.LoginAction.execute(LoginAction.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

登录操作:

public class LoginAction extends ActionSupport implements SessionAware, ModelDriven<User>{

private static final long serialVersionUID = -3369875299120377549L;

private User user = new User();
private SessionMap<String, Object> sessionAttributes = null;
private List<User> userList = new ArrayList<User>();
private UserDAO userDAO = new UserDAOImpl();


@Override
public String execute(){
System.out.println("inside execute");
if(userDAO.find(user.getName(),user.getPassword())){
//if(user.getName().equals(user.getName()) && user.getName().equals(user.getPassword())){
System.out.println("ggggggggggggg");
sessionAttributes.put("USER", user);
return SUCCESS;
}
return INPUT;
}
@Override
public void setSession(Map<String, Object> sessionAttributes) {
this.sessionAttributes = (SessionMap<String, Object>)sessionAttributes;
}

@Override
public User getModel() {
return user;
}

UserDaoImpl 类:

public class UserDAOImpl implements SessionAware,UserDAO {

@SessionTarget
Session session;

@TransactionTarget
Transaction transaction;

private Map<String, Object> sessionAttributes = null;

@Override
public void setSession(Map<String, Object> sessionAttributes) {
this.session = (Session) sessionAttributes;
}

@SuppressWarnings("unchecked")
public boolean find(String name, String password) {
System.out.println("inside find");
System.out.println("Nameeee "+name +" passwordddd "+password);
boolean result = false;
List<User> user = null;
try {
System.out.println("session getuser "+session);
String hql = "from User u where u.name = : name and u.password = : password";
user = session.createQuery(hql).list();

while (!user.isEmpty()) {
result = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

身份验证拦截器类:

public class AuthenticationInterceptor implements Interceptor {

private static final long serialVersionUID = -5011962009065225959L;

@Override
public void destroy() {
//release resources here
}

@Override
public void init() {
// create resources here
}

@Override
public String intercept(ActionInvocation actionInvocation)
throws Exception {
System.out.println("inside auth interceptor");
Map<String, Object> sessionAttributes = actionInvocation.getInvocationContext().getSession();

User user = (User) sessionAttributes.get("USER");

if(user == null){
return Action.LOGIN;
}else{
Action action = (Action) actionInvocation.getAction();
if(action instanceof UserAware){
((UserAware) action).setUser(user);
}
return actionInvocation.invoke();
}
}

}

欢迎操作类:

public class WelcomeAction extends ActionSupport implements UserAware, ModelDriven<User> {

private static final long serialVersionUID = 8111120314704779336L;

@Override
public String execute(){
return SUCCESS;
}

private User user;
@Override
public void setUser(User user) {
this.user=user;
}

public User getUser(User user){
return this.user;
}

@Override
public User getModel() {
return this.user;
}
}

Struts.xml

<struts>
<constant name="struts.mapper.action.prefix.enabled" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<!-- <constant name="struts.ui.theme" value="simple" /> -->
<package name="default" extends="hibernate-default">

<interceptors>
<interceptor name="authentication"
class="com.inwi.interceptors.AuthenticationInterceptor"></interceptor>
<interceptor-stack name="authStack">
<interceptor-ref name="authentication"></interceptor-ref>
<interceptor-ref name="defaultStackHibernate"></interceptor-ref>
</interceptor-stack>
</interceptors>

<default-interceptor-ref name="authStack"></default-interceptor-ref>

<global-results>
<result name="loginAction" type="redirect">/home.action</result>
</global-results>

<action name="home">
<interceptor-ref name="defaultStackHibernate"></interceptor-ref>
<result>/login.jsp</result>
</action>

<action name="loginAction" class="com.action.LoginAction">
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">/dashboard.jsp</result>
<result name="input">/loginError.jsp</result>
</action>
<action name="welcome" class="com.action.WelcomeAction">
<result name="success">/dashboard.jsp</result>
<!-- <result name="success">/siteMain.jsp</result> -->
</action>

<action name="logOut" method="logout" class="com.action.LoginAction">
<result name="success" type="redirect">/</result>
</action>

登录jsp:

<form action="loginAction" method="post" id="login_form">
<s:textfield id="username" name="name" placeholder="Username" required="required"/>
<s:password id="password" name="password" placeholder="Password" required="required" />
<button type="submit">Se connecter</button>
</form>

编辑1:

java.lang.NullPointerExceptioninside execute
inside find
Nameeee admin passwordddd admin
session getuser null

at com.inwi.dao.UserDAOImpl.find(UserDAOImpl.java:43)
at com.inwi.action.LoginAction.execute(LoginAction.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)

最佳答案

您的 SESSION 可以是null以及您尚未为参数分配值的其他内容。检查这两个内容

user = session.createQuery(hql).list();  
List result = session.createQuery(hql).setParameter("name","ARUN")..setParameter("password", "124").list();

关于java - struts2中使用拦截器时出现NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31860197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com