SSH整合问题 求高手解决
我用的是MyEclipse8.6+tomcat7
做的是struts2.1 + spring2.5 + hibernate3整合的小例子 ,用来练习。
添加的 所有支持均是myEclipse的可视化工具完成的。
login.jsp:
- HTML code
<form action="<%=request.getContextPath() %>/login.action">用户名:<input name="stu.stuName"><br>密码:<input name="stu.stuPwd"><br><input type="submit" value="submit"></form>
web.xml:
- XML code
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 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"> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <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> <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> </web-app>
struts.xml:
- XML code
<?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><package name="suibiaojiao" extends="struts-default"> <action name="login" class="loginAction"> <result name="ok">/ok.jsp</result> <result name="error">/error.jsp</result> </action> </package></struts>
applicationContext.xml:
- XML code
<?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-2.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://127.0.0.1:3306"></property> <property name="username" value="root"></property> <property name="password" value="lm"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="studentDAO" class="com.lm.Impdao.StudentDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="loginAction" class="com.lm.struts2.loginAction"> <property name="stuDAO" ref="studentDAO"></property> </bean></beans>
hibernate.cfg.xml:
- XML code
<?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.MySQLDialect </property> <property name="show_sql">true</property> <mapping resource="com/lm/dao/Student.hbm.xml" /> </session-factory></hibernate-configuration>loginAction:[code=Java]package com.lm.struts2;import com.lm.Idao.IStudentDAO;import com.lm.dao.Student;import com.opensymphony.xwork2.ActionSupport;public class loginAction extends ActionSupport { private Student stu; private IStudentDAO stuDAO; public Student getStu() { return stu; } public IStudentDAO getStuDAO() { return stuDAO; } public void setStu(Student stu) { this.stu = stu; } public void setStuDAO(IStudentDAO stuDAO) { this.stuDAO = stuDAO; } public String execute() throws Exception{ String stuName = stu.getStuName(); String stuPwd = stu.getStuPwd(); String tag="error"; System.out.println(stuName+" "+stuPwd); if("a".equals(stuName) && "a".equals(stuPwd)) tag="ok"; //stuDAO.save(new Student(stuName,stuPwd)); return tag; }}
在loginAction中,如果加上stuDAO.save(new Student(stuName,stuPwd));
这句代码,即如果调用操作数据库的方法就会报空指针异常错误,所以我判断是struts2和spring没有整合成功(如果整合成功的话stuDAO对象不会为空的),如果把操作数据库的代码注释掉(如上),页面却报了另一个错误:
HTTP Status 404 - No result defined for action com.lm.struts2.loginAction and result ok
我有两个问题:
1 为什么struts2和spring的整合不成功? 你们看我的配置文件哪里出错了吗?
2 HTTP Status 404 - No result defined for action com.lm.struts2.loginAction and result ok是不是说我没有配置ok.jsp,但是在struts2中的配置文件中已经声明了啊,并且我的项目中有ok.jsp的啊。
这两天在学习ssh整合,求帮忙啊。
[解决办法]
不要用myeclipse自动添加的包,自己手动去下所有的包,然后自己搭建。
[解决办法]
既然是学习就得一步一步去做,用可是化的你怎么知道怎么弄的。jar包自己网上下,自己导进去,配置文件根据官方说明自己写,这样一方面能明白过程,另一方面错了也知道那错了,没几个项目的配置让系统自己生成。web.xml的配置文件呢?里面配好了吗?
个人技术博客 :http://www.happyalaric.com