applicationContext.xml<bean id="userDao" class="com.quxiuna.dao.impl.UserDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="userService" class="com.quxiuna.service.impl.UserServiceImpl">
<property name="userDao">
<ref local="userDao" />
</property>
</bean>
<bean id="loginAction" class="com.quxiuna.struts.action.login.LoginAction" scope="prototype">
<property name="userService">
<ref local="userService" />
</property>
</bean>struts.xml<struts>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />
<constant name="struts.objectFactory.spring.autoWire" value="type" />
<include file="struts-default.xml"/>
<include file="com/quxiuna/struts/action/login/loginAction.xml" />
</struts> loginAction.xml<struts>
<package name="login" extends="struts-default" namespace="/login">
<action name="loginAction" class="com.quxiuna.struts.action.login.LoginAction">
<result name="success">/sendSuccss.jsp</result>
</action>
</package>
</struts> web.xml <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>UserServiceImpl.javapublic class UserServiceImpl implements UserService {
public UserDAO userDao; public UserDAO getUserDao() {
return userDao;
} public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}package com.quxiuna.struts.action.login;import java.util.Map;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.quxiuna.hibernate.po.RbacUser;
import com.quxiuna.service.UserService;
import com.quxiuna.util.Constants;public class LoginAction extends ActionSupport {
private static final long serialVersionUID = -1640288447822607013L;
private String name;
private String password;
private String message;
public UserService userService; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
@SuppressWarnings("unchecked")
public String execute() {
RbacUser user = userService.login(name, password);
if (user != null) {
ActionContext ctx = ActionContext.getContext();
Map session = ctx.getSession();
session.put(Constants.SESSSION_NAME, name);
return SUCCESS;
} else {
message = "用户名或密码错误!";
return ERROR;
}
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public UserService getUserService() {
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
}
}
报错
java.lang.NullPointerException
com.quxiuna.struts.action.login.LoginAction.execute(LoginAction.java:38)
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)
…………………………………………
…………………………………………
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="userService" class="com.quxiuna.service.impl.UserServiceImpl">
<property name="userDao">
<ref local="userDao" />
</property>
</bean>
<bean id="loginAction" class="com.quxiuna.struts.action.login.LoginAction" scope="prototype">
<property name="userService">
<ref local="userService" />
</property>
</bean>struts.xml<struts>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />
<constant name="struts.objectFactory.spring.autoWire" value="type" />
<include file="struts-default.xml"/>
<include file="com/quxiuna/struts/action/login/loginAction.xml" />
</struts> loginAction.xml<struts>
<package name="login" extends="struts-default" namespace="/login">
<action name="loginAction" class="com.quxiuna.struts.action.login.LoginAction">
<result name="success">/sendSuccss.jsp</result>
</action>
</package>
</struts> web.xml <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>UserServiceImpl.javapublic class UserServiceImpl implements UserService {
public UserDAO userDao; public UserDAO getUserDao() {
return userDao;
} public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}package com.quxiuna.struts.action.login;import java.util.Map;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.quxiuna.hibernate.po.RbacUser;
import com.quxiuna.service.UserService;
import com.quxiuna.util.Constants;public class LoginAction extends ActionSupport {
private static final long serialVersionUID = -1640288447822607013L;
private String name;
private String password;
private String message;
public UserService userService; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
@SuppressWarnings("unchecked")
public String execute() {
RbacUser user = userService.login(name, password);
if (user != null) {
ActionContext ctx = ActionContext.getContext();
Map session = ctx.getSession();
session.put(Constants.SESSSION_NAME, name);
return SUCCESS;
} else {
message = "用户名或密码错误!";
return ERROR;
}
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public UserService getUserService() {
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
}
}
报错
java.lang.NullPointerException
com.quxiuna.struts.action.login.LoginAction.execute(LoginAction.java:38)
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)
…………………………………………
…………………………………………
解决方案 »
- 自己写的一棵前端树状展现,界面已经美化,仿windows,并开源。
- 有关tomcat5.5数据连接池配置问题。。。
- 希望大家可以讨论一下java的内容管理系统和php的发展前景
- 大姐求救:我用JBOSS做服务器,我的程序在192.168.12.2/rlzy下,如用户不小心输入了192.168.12.2,系统自动进入了JBOSS控制台,这样很不安
- struts中的tiles问题 急。。。大家帮忙
- [求助]我用了一个JSP的统计系统,但不可以在html文件里调用
- 谁有关于java bean的入门书吗?
- 一个开发方式的选向问题
- 怎样找工作?
- servlet如果输出一个图像到一个web page?300分
- Ext iframe问题
- The server encountered an internal error () that prevented it from fulfilling th
这样会报错严重: Error filterStart
2010-3-23 17:04:38 org.apache.catalina.core.StandardContext start
严重: Context [/email] startup failed due to previous errors
2010-3-23 17:04:38 org.apache.coyote.http11.Http11BaseProtocol start
spring加载了,loginAction对象也生成并且注入成功了,但是和struts2的使用的loginAction对象不是一个,所以报空指针错误。把完整的web.xml发上来看看吧
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- OpenSessionInViewFilter过滤器 -->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
web.xml里的Action的class=“spring里配置的Action的name”
<bean id="loginAction" class="com.quxiuna.struts.action.login.LoginAction" scope="prototype">
<property name="userService">
<ref local="userService" />
</property>
</bean>
删掉试试
com.quxiuna.struts.action.login.LoginAction.execute(LoginAction.java:38)错误多明确。。
那就看看 38行呀。RbacUser user = userService.login(name, password);
其中 userService 你初始化了吗?
sorry...习惯@Autowired了
不过 肯定还是38行那个有问题
这就话说明你把action交给spring管理了,
<action name="loginAction" class="com.quxiuna.struts.action.login.LoginAction">
这句话的意思是让struts去管理action,这样就矛盾了,
<bean id="loginAction" class="com.quxiuna.struts.action.login.LoginAction" scope="prototype">
这句话又是让spring管理action,又矛盾了
应该改成:
<action name="loginAction" class="LoginAction">
其他不变~~~
<bean id="loginAction" class="com.quxiuna.struts.action.login.LoginAction" scope="prototype">
<property name="userService">
<ref local="userService" />
</property>
</bean>
上面的id 建议换成name="/loginAction" 和下面的struts中的action的path对应
<struts>
<package name="login" extends="struts-default" namespace="/login"> <action name="loginAction" class="com.quxiuna.struts.action.login.LoginAction">
这里的class不要。通过spring注入不需要class,已经和上面的bean对应了。
<result name="success">/sendSuccss.jsp </result> </action>
</package>
</struts>
对于注入的属性,你能不提供get方法不? 这也可能导致问题的.以前我就碰到过.虽然不是注入的问题,为什么要多提个一个get方法的? 偷下懒???
public UserService userService;
的 get/set 方法
如果还报错,那是别的错误,贴出来
问题在struts.xml 中
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />不是矛盾了吗, 对象到底由谁创建