applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/ffms/hbm/Userinfo.hbm.xml</value></list>
</property></bean>
<bean id="userinfoDAO" class="com.ffms.dao.UserinfoDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!--<bean id="loginService" class="com.ffms.service.LoginService">
<property name="uifd" ref="UserinfoDAO"></property>
</bean>
-->
<bean id="userAction1" class="com.ffms.action.UserAction">
<property name="userInfoDAO1" ref="userinfoDAO"></property>
</bean>
</beans>UserAction.javapackage com.ffms.action;import com.ffms.dao.UserinfoDAO;
import com.ffms.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {
private LoginService loginService ;
private UserinfoDAO userInfoDAO1 ;
public String execute() throws Exception {
System.out.println("11");
//loginService.getUserInfoList();
userInfoDAO1.findAll();
return SUCCESS;
} public void setLoginService(LoginService loginService) {
this.loginService = loginService;
} public LoginService getLoginService() {
return loginService;
} public UserinfoDAO getUserInfoDAO1() {
return userInfoDAO1;
} public void setUserInfoDAO1(UserinfoDAO userInfoDAO1) {
this.userInfoDAO1 = userInfoDAO1;
}
}
UserinfoDAO.javapackage com.ffms.dao;import java.util.List;import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.ffms.pojo.Userinfo;/**
* A data access object (DAO) providing persistence and search support for Userinfo entities.
* Transaction control of the save(), update() and delete() operations
can directly support Spring container-managed transactions or they can be augmented to handle user-managed Spring transactions.
Each of these methods provides additional information for how to configure it for the desired type of transaction control.
* @see com.ffms.pojo.Userinfo
* @author MyEclipse Persistence Tools
*/public class UserinfoDAO extends HibernateDaoSupport {
private static final Logger log = LoggerFactory.getLogger(UserinfoDAO.class);
//property constants
public static final String USER_NAME = "userName";
public static final String PASS_WORD = "passWord"; protected void initDao() {
//do nothing
}
public void save(Userinfo transientInstance) {
log.debug("saving Userinfo instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Userinfo persistentInstance) {
log.debug("deleting Userinfo instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Userinfo findById( java.lang.Integer id) {
log.debug("getting Userinfo instance with id: " + id);
try {
Userinfo instance = (Userinfo) getHibernateTemplate()
.get("com.ffms.pojo.Userinfo", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Userinfo instance) {
log.debug("finding Userinfo instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding Userinfo instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Userinfo as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
} public List findByUserName(Object userName
) {
return findByProperty(USER_NAME, userName
);
}
public List findByPassWord(Object passWord
) {
return findByProperty(PASS_WORD, passWord
);
}
public List findAll() {
log.debug("finding all Userinfo instances");
try {
String queryString = "from Userinfo";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Userinfo merge(Userinfo detachedInstance) {
log.debug("merging Userinfo instance");
try {
Userinfo result = (Userinfo) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public void attachDirty(Userinfo instance) {
log.debug("attaching dirty Userinfo instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Userinfo instance) {
log.debug("attaching clean Userinfo instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public static UserinfoDAO getFromApplicationContext(ApplicationContext ctx) {
return (UserinfoDAO) ctx.getBean("UserinfoDAO");
}
public static void main(String[] args) {
UserinfoDAO udao = new UserinfoDAO();
List<Userinfo> list = udao.findAll();
for (int i = 0; i <list.size(); i++) {
System.out.println(list.get(i));
}
}
}
报如下错
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
com.ffms.action.UserAction.execute(UserAction.java:14)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)请各位高手帮忙解决,谢谢了。
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/ffms/hbm/Userinfo.hbm.xml</value></list>
</property></bean>
<bean id="userinfoDAO" class="com.ffms.dao.UserinfoDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!--<bean id="loginService" class="com.ffms.service.LoginService">
<property name="uifd" ref="UserinfoDAO"></property>
</bean>
-->
<bean id="userAction1" class="com.ffms.action.UserAction">
<property name="userInfoDAO1" ref="userinfoDAO"></property>
</bean>
</beans>UserAction.javapackage com.ffms.action;import com.ffms.dao.UserinfoDAO;
import com.ffms.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {
private LoginService loginService ;
private UserinfoDAO userInfoDAO1 ;
public String execute() throws Exception {
System.out.println("11");
//loginService.getUserInfoList();
userInfoDAO1.findAll();
return SUCCESS;
} public void setLoginService(LoginService loginService) {
this.loginService = loginService;
} public LoginService getLoginService() {
return loginService;
} public UserinfoDAO getUserInfoDAO1() {
return userInfoDAO1;
} public void setUserInfoDAO1(UserinfoDAO userInfoDAO1) {
this.userInfoDAO1 = userInfoDAO1;
}
}
UserinfoDAO.javapackage com.ffms.dao;import java.util.List;import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.ffms.pojo.Userinfo;/**
* A data access object (DAO) providing persistence and search support for Userinfo entities.
* Transaction control of the save(), update() and delete() operations
can directly support Spring container-managed transactions or they can be augmented to handle user-managed Spring transactions.
Each of these methods provides additional information for how to configure it for the desired type of transaction control.
* @see com.ffms.pojo.Userinfo
* @author MyEclipse Persistence Tools
*/public class UserinfoDAO extends HibernateDaoSupport {
private static final Logger log = LoggerFactory.getLogger(UserinfoDAO.class);
//property constants
public static final String USER_NAME = "userName";
public static final String PASS_WORD = "passWord"; protected void initDao() {
//do nothing
}
public void save(Userinfo transientInstance) {
log.debug("saving Userinfo instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Userinfo persistentInstance) {
log.debug("deleting Userinfo instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Userinfo findById( java.lang.Integer id) {
log.debug("getting Userinfo instance with id: " + id);
try {
Userinfo instance = (Userinfo) getHibernateTemplate()
.get("com.ffms.pojo.Userinfo", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Userinfo instance) {
log.debug("finding Userinfo instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding Userinfo instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Userinfo as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
} public List findByUserName(Object userName
) {
return findByProperty(USER_NAME, userName
);
}
public List findByPassWord(Object passWord
) {
return findByProperty(PASS_WORD, passWord
);
}
public List findAll() {
log.debug("finding all Userinfo instances");
try {
String queryString = "from Userinfo";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Userinfo merge(Userinfo detachedInstance) {
log.debug("merging Userinfo instance");
try {
Userinfo result = (Userinfo) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public void attachDirty(Userinfo instance) {
log.debug("attaching dirty Userinfo instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Userinfo instance) {
log.debug("attaching clean Userinfo instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public static UserinfoDAO getFromApplicationContext(ApplicationContext ctx) {
return (UserinfoDAO) ctx.getBean("UserinfoDAO");
}
public static void main(String[] args) {
UserinfoDAO udao = new UserinfoDAO();
List<Userinfo> list = udao.findAll();
for (int i = 0; i <list.size(); i++) {
System.out.println(list.get(i));
}
}
}
报如下错
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
com.ffms.action.UserAction.execute(UserAction.java:14)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)请各位高手帮忙解决,谢谢了。
解决方案 »
- 如何在jsp中内嵌的java代码中获得struts2 action传过来的对象的变量
- tomcat项目移植weblogic9的问题,高手进
- 如何获取字段hudong_id 的值并放到表2的hudong_id里面
- 100分求数据总和,100分在线等~~~
- 很简单的 改错题 (为什么啊??)
- 关于JSTL的中文表单提交的问题,帮帮我~~
- 紧急呼救!紧急呼救!紧急呼救!紧急呼救!
- 请问连接数据库的bean
- 菜鸟问题:表单提交后收不到任何变量
- 用的是ssh框架,在导出的时候。报的这个错误,但是不影响导出。
- 我是菜鸟,诚心请教jsp+javabean+servlet的问题!
- The requested resource is not available
或许能帮你
@Autowire
private LoginService loginService ;
private UserinfoDAO userInfoDAO1 ;
第二种,从context种获取:private LoginService loginService;
public void execute() {
loginService = ApplicationContext.getBean("loginService");
}
main方法:
ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserAction mUserAction = (UserAction)factory.getBean("userAction1");
mUserAction.execute();你的action:
public String execute() throws Exception {
System.out.println("11");
// loginService.getUserInfoList();
userInfoDAO1.findAll();
System.out.println("22");
return SUCCESS;
}打印,输出:
2012-11-18 12:59:05 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1301ed8: startup date [Sun Nov 18 12:59:05 CST 2012]; root of context hierarchy
2012-11-18 12:59:06 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
2012-11-18 12:59:07 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1f64158: defining beans [dataSource,sessionFactory,userinfoDAO,userAction1]; root of factory hierarchy
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2012-11-18 12:59:09 org.springframework.orm.hibernate3.LocalSessionFactoryBean buildSessionFactory
信息: Building new Hibernate SessionFactory
11
22
你好,我直接用浏览器打开调用userAction不知道为什么报空指针异常,小弟刚接触ssh,麻烦你帮忙看看,谢谢了。
你好 ,我改完后还是一样的,(小弟刚接触ssh,烦请帮忙解决下)
你是说这个里面配置错了吗?应该怎么配置?之前:
<bean id="userAction1" class="com.ffms.action.UserAction">
<property name="userInfoDAO1" ref="userinfoDAO"></property>
</bean>
命名很不规范,看着看类,字母有时候大写有时候小写,,
这么配置很误导人,如果装配方式使用类型装配就行不通了,但是我没有看到。。
还是规范点的好。
另外 Action中直接注入dao不好,不符合dao模式规范。userInfoDAO1为空 ,你把代码改规范之后在看看。。