自学STRUTS2+SPRING3+HIBERNATE3 发现问题无法解决,贴出以下代码1、applicationContext.xml:
...
<!-- 配置Hibernate 数据源-->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/xyssh" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 配置Hibernate SeesionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="myDataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean> <!-- 配置其他beans-->
<bean id="LoginAction" class="com.xyssh.system.action.LoginAction"
scope="prototype">
<property name="userDao" ref="UserDao"></property>
</bean>
<bean id="UserDao" class="com.xyssh.system.dao.UserDao" scope="prototype">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>2、struts.xml:
<struts>
<package name="user" namespace="/" extends="struts-default">
<action name="login" class="com.xyssh.system.action.LoginAction" >
<result>
<param name="success">/login.jsp</param>
<param name="error">/index.jsp</param>
</result>
</action>
</package>
</struts>
3、web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 4、LoginAction.java
public class LoginAction extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = -3945074126919510909L; private String code;
private String password;
private UserDao userDao;
public String execute() {
//查找用户
List<User> users = getUserDao().findByProperty("code", code);
if (users.size() == 0){
return "error";
}
User user = users.get(0);
//判断密码
if (user.getPassword().equals(password)){
return "error";
}
return "success";
} public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public UserDao getUserDao() {
return userDao;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}5、UserDao.java:
public class UserDao extends HibernateDaoSupport {
......
public List<User> findByProperty(String propertyName, Object value) {
log.debug("finding USER instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from USER as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}6、遇到的问题:
当运行到LoginAction.execute方法时,报错,原因为getUserDao()为null,如果我在这里new UserDao,则在UserDao中的getHibernateTemplate()为null,恳请各位给新手解决方法,谢谢!
...
<!-- 配置Hibernate 数据源-->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/xyssh" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 配置Hibernate SeesionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="myDataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean> <!-- 配置其他beans-->
<bean id="LoginAction" class="com.xyssh.system.action.LoginAction"
scope="prototype">
<property name="userDao" ref="UserDao"></property>
</bean>
<bean id="UserDao" class="com.xyssh.system.dao.UserDao" scope="prototype">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>2、struts.xml:
<struts>
<package name="user" namespace="/" extends="struts-default">
<action name="login" class="com.xyssh.system.action.LoginAction" >
<result>
<param name="success">/login.jsp</param>
<param name="error">/index.jsp</param>
</result>
</action>
</package>
</struts>
3、web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 4、LoginAction.java
public class LoginAction extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = -3945074126919510909L; private String code;
private String password;
private UserDao userDao;
public String execute() {
//查找用户
List<User> users = getUserDao().findByProperty("code", code);
if (users.size() == 0){
return "error";
}
User user = users.get(0);
//判断密码
if (user.getPassword().equals(password)){
return "error";
}
return "success";
} public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public UserDao getUserDao() {
return userDao;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}5、UserDao.java:
public class UserDao extends HibernateDaoSupport {
......
public List<User> findByProperty(String propertyName, Object value) {
log.debug("finding USER instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from USER as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}6、遇到的问题:
当运行到LoginAction.execute方法时,报错,原因为getUserDao()为null,如果我在这里new UserDao,则在UserDao中的getHibernateTemplate()为null,恳请各位给新手解决方法,谢谢!
解决方案 »
- Ext.form.DateField之间的传值----超急
- 我要孫MM的精通hibernate源代碼(只要代碼,不要整個光盤)
- 数据源的配置JNDI FSContext
- 请教大家一个问题哦,请大家说一下HTML里的上传图片文件的控件是?
- 请问这是什么原因package java.servlet does not exist
- JSP中是否可以在URL附带的请求参数中引用变量
- 请高手指点一下我这个java的初哥,关于java调用oracle储存过程的问题?
- .js代码,请告诉我是什么意思?
- 如何从中途中断jsp的执行?
- java web
- 新手初学,求J2EE项目源码研究
- 求个牛牛面粉厂项目数据库和源代码
<action name="login" class="LoginAction" > //改成这样--交由spring管理,
还有,struts2整合spring 应该要加
struts2-spring-plugin-2.1.8.1.jar
<constant name="struts.objectFactory" value="spring" />然后将:action name="login" class="com.xyssh.system.action.LoginAction" >
改成:action name="login" class="LoginAction" >记得要加上struts-spring的一个插件jar包进去!名字是类似:struts2-spring-plugin-2.0.14.jar,只是版本号不一样而已。
<struts>
<package name="user" namespace="/" extends="struts-default">
<action name="login" class="LoginAction" >
<result>
<param name="success">/login.jsp</param>
<param name="error">/index.jsp</param>
</result>
</action> </package>
<constant name="struts.objectFactory" value="spring" />
</struts>
在web.xml<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
后面加上
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml</param-value>
</context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml</param-value>
</context-param>还是这个观察仔细
scope="prototype">
<property name="userDao" ref="UserDao"></property>
</bean>你配的这是什么玩意···