ssh整合时报错java.lang.NullPointerException 求高手解答,急急急
java.lang.NullPointerExceptioncom.pb.action.StudentAction.login(StudentAction.java:34...
java.lang.NullPointerException
com.pb.action.StudentAction.login(StudentAction.java:34)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
struts配置文件
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.pb.action.StudentAction" method="login">
<result name="login">Welcome.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
spring配置文件
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="studentDao" class="com.pb.dao.impl.StudentDaoImpl">
<property name="sessionFactory" ref="sessionFactory">
</property>
</bean>
<bean id="studentService" class="com.pb.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao">
</property>
</bean>
<bean id="StudentAction" class="com.pb.action.StudentAction" scope="prototype">
<property name="studentService" ref="studentService">
</property>
</bean>
<bean id="TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
daoImpl文件
public class StudentDaoImpl extends HibernateDaoSupport implements StudentDao {
@Override
public void saveStudent(Student student) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(student);
}
@Override
public List login(Student student) {
// TODO Auto-generated method stub
String hql="from Student s where s.name=? and s.password=?";
Object value[]={student.getName(),student.getPassword()};
List<Student> list=this.getHibernateTemplate().find(hql,value);
return list;
}
}
action文件
public class StudentAction extends ActionSupport {
StudentService studentService;
Student student;
public void setStudent(Student student) {
this.student = student;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public String login()
{
Student s = new Student();
s.setName(student.getName());
s.setPassword(student.getPassword());
System.out.println(s.getName()+s.getPassword());//(这里输出不为空)
boolean login=studentService.login(s);//这里报错
if(login)
return "login";
else
return "error";
}
页面文件
<body>
<s:form action="login">
<s:textfield key="username" name="student.name"/>
<s:password key="password" name="student.password"/>
<s:submit/>
</s:form>
</body>
之前在测试类中添加用户可以(图),加入action就报错,在知道里看了3页,可能是action中注入studentService的问题,但无法解决,坐等高手解答 展开
com.pb.action.StudentAction.login(StudentAction.java:34)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
struts配置文件
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.pb.action.StudentAction" method="login">
<result name="login">Welcome.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
spring配置文件
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="studentDao" class="com.pb.dao.impl.StudentDaoImpl">
<property name="sessionFactory" ref="sessionFactory">
</property>
</bean>
<bean id="studentService" class="com.pb.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao">
</property>
</bean>
<bean id="StudentAction" class="com.pb.action.StudentAction" scope="prototype">
<property name="studentService" ref="studentService">
</property>
</bean>
<bean id="TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
daoImpl文件
public class StudentDaoImpl extends HibernateDaoSupport implements StudentDao {
@Override
public void saveStudent(Student student) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(student);
}
@Override
public List login(Student student) {
// TODO Auto-generated method stub
String hql="from Student s where s.name=? and s.password=?";
Object value[]={student.getName(),student.getPassword()};
List<Student> list=this.getHibernateTemplate().find(hql,value);
return list;
}
}
action文件
public class StudentAction extends ActionSupport {
StudentService studentService;
Student student;
public void setStudent(Student student) {
this.student = student;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public String login()
{
Student s = new Student();
s.setName(student.getName());
s.setPassword(student.getPassword());
System.out.println(s.getName()+s.getPassword());//(这里输出不为空)
boolean login=studentService.login(s);//这里报错
if(login)
return "login";
else
return "error";
}
页面文件
<body>
<s:form action="login">
<s:textfield key="username" name="student.name"/>
<s:password key="password" name="student.password"/>
<s:submit/>
</s:form>
</body>
之前在测试类中添加用户可以(图),加入action就报错,在知道里看了3页,可能是action中注入studentService的问题,但无法解决,坐等高手解答 展开
3个回答
展开全部
<action name="login" class="com.pb.action.StudentAction" method="login">
<result name="login">Welcome.jsp</result>
<result name="error">error.jsp</result>
</action>
你的struts配置文件里,这个class这么写属于单独配置login引用的类。但实际上这个类所需要的引用来自spring。你的class中的值应该写spring配置文件中bean的id.
另外需要引用一个struts-spring-plugin的jar包。jar包差不多是这个名字 记不太清了
<result name="login">Welcome.jsp</result>
<result name="error">error.jsp</result>
</action>
你的struts配置文件里,这个class这么写属于单独配置login引用的类。但实际上这个类所需要的引用来自spring。你的class中的值应该写spring配置文件中bean的id.
另外需要引用一个struts-spring-plugin的jar包。jar包差不多是这个名字 记不太清了
更多追问追答
追问
你好,首先struts2-spring-plugin-2.1.6.jar这个包我是有的,按你的意思我spring配置文件中不改
在struts配置文件中改为
不知道我理解的对不对,但运行时报错Unable to instantiate Action, StudentAction, defined for 'login' in namespace '/'StudentAction
java.lang.ClassNotFoundException: StudentAction
org.apache.catalina.loader.WebappClass。。。
追答
我觉得这么写没什么问题。不过下边给的异常是找不到类,所以我觉得你可能缺少其他的jar包。或者你的jar包版本不对
展开全部
StudentService studentService;
Student student;
把这两个要生成getter方法 ,你值生成了setter方法,setter方法是往页面传值,getter是从页面获取值
Student student;
把这两个要生成getter方法 ,你值生成了setter方法,setter方法是往页面传值,getter是从页面获取值
更多追问追答
追问
是的,我是有get方法的只是因为字数的限制,我没贴出来,sorry,在帮我看看
追答
你action已经注入service类了! 不知道你这个StudentService 是接口还是类?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你再好好看下你配置的类的路径吧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询