这个是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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-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">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- 指定连接数据库的驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl" value="jdbc:mysql://192.168.1.102:3306/test"/>
<!-- 指定连接数据库的用户名 -->
<property name="user" value="root"/>
<!-- 指定连接数据库的密码 -->
<property name="password" value="mysqladmin"/>
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="40"/>
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1"/>
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="1"/>
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20"/>
</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/ssh/user/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="UserDao" class="com.ssh.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="UserService" class="com.ssh.service.impl.UserServiceImpl">
<property name="userDAO" ref="UserDao" />
</bean>
<!-- name属性对应.do的名字 -->
<bean name="/user" class="com.ssh.struts.action.UserAction" scope="prototype">
<property name="userService" ref="IUserService" />
</bean>
</beans>
------------------------------------
这个是struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="userForm" type="com.ssh.struts.form.UserForm" />  </form-beans>  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="userForm"
      input="/form/user.jsp"
      name="userForm"
      parameter="method"
      path="/user"
      scope="request"
      type="com.ssh.struts.action.UserAction">
      <forward name="init" path="/jsp/userList.jsp"></forward>
    </action>  </action-mappings>  <message-resources parameter="com.ssh.struts.ApplicationResources" />
</struts-config>
-----------------------------------
这个是action
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.ssh.struts.action;import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;import com.ssh.service.IUserService;
import com.ssh.struts.form.UserForm;/**
 * MyEclipse Struts Creation date: 08-26-2010
 * 
 * XDoclet definition:
 * 
 * @struts.action path="/user" name="userForm" input="/form/user.jsp"
 *                parameter="method" scope="request" validate="true"
 */
public class UserAction extends DispatchAction {    private IUserService userService;    /**
     * 初始化方法
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward init(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    
UserForm userForm = (UserForm) form;// TODO Auto-generated method stub

List list = null;
try {
list = userService.getAllUser();
} catch (Exception e) {
    e.printStackTrace();
} request.setAttribute("list", list);
return mapping.findForward("init");
    }    /**
     * Method execute
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward save(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
return mapping.findForward("save");
    }    public ActionForward delete(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
return mapping.findForward("delete");
    }    public ActionForward update(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
return mapping.findForward("update");
    }    public ActionForward queryByName(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
return mapping.findForward("queryByName");
    }    
    
    /*****************华丽的分割线  下面是注入方法******************************/
    public IUserService getUserService() { return userService;
    }    
    public void setUserService(IUserService userService) {
    
        this.userService = userService;
    }}
-------------------------------
这个是web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <init-param>
   <param-name>debug</param-name>
   <param-value>3</param-value>
  </init-param>
  <init-param>
   <param-name>detail</param-name>
   <param-value>3</param-value>
  </init-param>
  <load-on-startup>0</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>
------------------------------
jsp页面就点了一个请求超链接
<a href="./user.do?method=init">去用户列表</a>
然后页面404了,MyEclipse报错
java.lang.NullPointerException
at com.ssh.struts.action.UserAction.init(UserAction.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Thread.java:595)希望高人解答在线等,小弟刚学不久,希望可以回答的容易理解一下,谢谢

解决方案 »

  1.   

    你确定 “return mapping.findForward("init");
    ”有与之对应的jsp页面吗?
      

  2.   

    有可能是userService.getAllUser();的问题,你调试一下.
      

  3.   

    有可能是userService为null。lz看看。。
      

  4.   

    <forward name="init" path="/jsp/userList.jsp"></forward>,漏楼主的jsp页面有没有放错地方?报的404应该是路径错误的
      

  5.   

    建议:
    第一 :  以后 注意命名的 时候 不要和 一些内部 方法 重名 比如 init destroy service 。
    第二: 遇到这种 404  500  这些简单的错误 要用断点调试你发帖的 这一段时间  如果用断电的话 
    估计 就已经解决了。。
      

  6.   


    <action
      attribute="userForm"
      input="/form/user.jsp"
      name="userForm"  
      parameter="method"
      path="/user"  --提交form中的的action名字
      scope="request"
      type="com.ssh.struts.action.UserAction">
      <forward name="init" path="/jsp/userList.jsp"></forward>
      </action>
    看着你的有点乱,你的问题就是action的问题,你运行.do  让它先走action  估计都跳错了!
    debug走走看