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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置Hibernate的数据库连接,托管给Spring来生成SessionFactory对象 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="loginDAO" class="com.gw.dao.impl.LoginDAOImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="loginService" class="com.gw.service.impl.LoginServiceImpl">
<property name="dao" ref="loginDAO"></property>
</bean>
<bean name="loginBean" class="com.gw.action.LoginAction">
<property name="service" ref="loginService"></property>
</bean>
<!-- 进行事务的配置,使用Aop方式进行声明 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<aop:config proxy-target-class="true"/>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.gw.service..*ServiceImpl.*(..))"/>
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
sturts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.objectFactory" value="spring"></constant>
<package name="default" extends="struts-default">
<action name="loginAction" class="com.gw.action.LoginAction">
<result name="success">/menu.jsp</result>
<result name="fail">/error.jsp</result>
</action>
</package>
<include file="struts_teacher.xml"></include>
</struts>
hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<property name="connection.username">wwhzz</property>
<property name="connection.password">abc</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="myeclipse.connection.profile">mydb</property>
<property name="c3p0.max_size">50</property>
<property name="c3p0.max_statements">30</property>
<property name="c3p0.min_size">20</property>
<property name="c3p0.timeout">18000</property>
<mapping resource="com/gw/pojo/Login.hbm.xml" />
</session-factory>
</hibernate-configuration>
LoginAction.java:
package com.gw.action;
import com.gw.pojo.Login;
import com.gw.service.LoginService;
public class LoginAction {
private String userName;
private String pwd;
private LoginService service;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public LoginService getService() {
return service;
}
public void setService(LoginService service) {
this.service = service;
}
public String execute(){
System.out.println(userName);
Login login = service.getLogin(userName, pwd);
if(login instanceof Login){
return "success";
}else{
return "fail";
}
}
LoginDAO.java:就一接口省略了
LoginDAOImpl.java:
package com.gw.dao.impl;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.gw.dao.LoginDAO;
import com.gw.pojo.Login;
public class LoginDAOImpl extends HibernateDaoSupport implements LoginDAO {
public Login getLogin(String userName, String pwd)
throws DataAccessException {
// TODO Auto-generated method stub
String hql = "from Login where loginName='"+userName+"' and loginPwd="+pwd;
return (Login)this.getHibernateTemplate().find(hql).get(0);
}
}
LoginService.xml:同LoginDAO.java
LoginService.xml:
package com.gw.service.impl;
import org.springframework.dao.DataAccessException;
import com.gw.dao.LoginDAO;
import com.gw.pojo.Login;
import com.gw.service.LoginService;
public class LoginServiceImpl implements LoginService {
private LoginDAO dao;
public LoginDAO getDao() {
return dao;
}
public void setDao(LoginDAO dao) {
this.dao = dao;
}
public Login getLogin(String userName, String pwd)
throws DataAccessException {
// TODO Auto-generated method stub
return dao.getLogin(userName, pwd);
}
}
界面就一个“用户名”和“密码”还有“登录”按钮,想通过上面的内容跳到主页面,当点击登录时,出现了下面的情况:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
com.gw.action.LoginAction.execute(LoginAction.java:32)
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)
....
服务器用的Tomcat,想用ssh整合
不晓得哪里出了问题,这是小弟做的毕业设计,遇到了这个问题实在不知道怎么解决,大侠们伸出援助之手吧
3 个解决方案
#1
肿么没人理额的贴纸呢,求大侠,求援手,自己占下位
#2
<!-- 这里已近指明了使用Spring来管理action类-->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="default" extends="struts-default">
<!-- 这里你又让struts直接创建这个类,这样LoginAction中的service当然为空了。 -->
<action name="loginAction" class="com.gw.action.LoginAction">
<result name="success">/menu.jsp</result>
<result name="fail">/error.jsp</result>
</action>
.......
正确配置如下:
<constant name="struts.objectFactory" value="spring"></constant>
<package name="default" extends="struts-default">
<!-- class属性使用springbean的名称,有Spring容器获取 -->
<action name="loginAction" class="loginBean">
<result name="success">/menu.jsp</result>
<result name="fail">/error.jsp</result>
</action>
.......
#3
谢谢,2楼大虾,就是这个问题,小弟膜拜
看来还是学艺不精啊
#1
肿么没人理额的贴纸呢,求大侠,求援手,自己占下位
#2
<!-- 这里已近指明了使用Spring来管理action类-->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="default" extends="struts-default">
<!-- 这里你又让struts直接创建这个类,这样LoginAction中的service当然为空了。 -->
<action name="loginAction" class="com.gw.action.LoginAction">
<result name="success">/menu.jsp</result>
<result name="fail">/error.jsp</result>
</action>
.......
正确配置如下:
<constant name="struts.objectFactory" value="spring"></constant>
<package name="default" extends="struts-default">
<!-- class属性使用springbean的名称,有Spring容器获取 -->
<action name="loginAction" class="loginBean">
<result name="success">/menu.jsp</result>
<result name="fail">/error.jsp</result>
</action>
.......
#3
谢谢,2楼大虾,就是这个问题,小弟膜拜
看来还是学艺不精啊