代码如下:
UserAction.javapackage com.amaker.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;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 org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;import com.amaker.beans.User;
import com.amaker.dao.UserDao;
import com.amaker.form.UserForm;public class UserAction extends DispatchAction {
   UserDao dao; public void setDao(UserDao dao) {
this.dao = dao;
} public ActionForward login(ActionMapping m, ActionForm f,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("111");
/* ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");*/
   ApplicationContext ac=WebApplicationContextUtils.
   getWebApplicationContext(this.getServlet().getServletContext());   
   dao = (UserDao) ac.getBean("userDao");
System.out.println("222");
UserForm userForm = (UserForm) f;
String username = userForm.getUsername();
String password = userForm.getPassword();
System.out.println("333");
User u = dao.login(username, password);
System.out.println("444");
if (u != null) {
HttpSession session = request.getSession();
session.setAttribute("userSession", u);
System.out.println("55555");
return m.findForward("success");
} else {
String msg = "用户名不能为空或空格或用户名、年龄不正确";
     request.setAttribute("tip", msg);
return m.getInputForward();
}
} public ActionForward regedit(ActionMapping m, ActionForm f,
HttpServletRequest request, HttpServletResponse response) {
/*
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");*/

    ApplicationContext ac=WebApplicationContextUtils.
    getWebApplicationContext(this.getServlet().getServletContext());   
    
    dao = (UserDao) ac.getBean("userDao");

UserForm userForm = (UserForm) f;
String username = userForm.getUsername();
String password = userForm.getPassword();
        int age =userForm.getAge();
User u = new User();
u.setUsername(username);
u.setPassword(password);
u.setAge(age);
dao.register(u);
HttpSession session = request.getSession();
session.setAttribute("userSession", u);
return m.findForward("success");
}
}
package com.amaker.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;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 org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;import com.amaker.beans.User;
import com.amaker.dao.UserDao;
import com.amaker.form.UserForm;public class UserAction extends DispatchAction {
   UserDao dao; public void setDao(UserDao dao) {
this.dao = dao;
} public ActionForward login(ActionMapping m, ActionForm f,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("111");
/* ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");*/
   ApplicationContext ac=WebApplicationContextUtils.
   getWebApplicationContext(this.getServlet().getServletContext());   
   dao = (UserDao) ac.getBean("userDao");
System.out.println("222");
UserForm userForm = (UserForm) f;
String username = userForm.getUsername();
String password = userForm.getPassword();
System.out.println("333");
User u = dao.login(username, password);
System.out.println("444");
if (u != null) {
HttpSession session = request.getSession();
session.setAttribute("userSession", u);
System.out.println("55555");
return m.findForward("success");
} else {
String msg = "用户名不能为空或空格或用户名、年龄不正确";
     request.setAttribute("tip", msg);
return m.getInputForward();
}
} public ActionForward regedit(ActionMapping m, ActionForm f,
HttpServletRequest request, HttpServletResponse response) {
/*
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");*/

    ApplicationContext ac=WebApplicationContextUtils.
    getWebApplicationContext(this.getServlet().getServletContext());   
    
    dao = (UserDao) ac.getBean("userDao");

UserForm userForm = (UserForm) f;
String username = userForm.getUsername();
String password = userForm.getPassword();
        int age =userForm.getAge();
User u = new User();
u.setUsername(username);
u.setPassword(password);
u.setAge(age);
dao.register(u);
HttpSession session = request.getSession();
session.setAttribute("userSession", u);
return m.findForward("success");
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!--<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
--><bean id="baseDao" class="com.amaker.common.dao.impl.BaseDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <bean id="userDao" class="com.amaker.dao.impl.UserDaoImpl"><!--
<property name="jdbcTemplate" ref="jdbcTemplate" />
--><property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--<bean id="userAction" class="com.amaker.action.UserAction" scope="prototype">
<property name="dao" ref="userDao"></property>
</bean>

-->
<bean name="/user"  class="com.amaker.action.UserAction"  scope="prototype" >
        <property name="dao">
            <ref local="userDao" />
        </property>
     </bean>
 <bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/spring_db">
</property>
<property name="username" value="root"></property>
<property name="password" value="1"></property>
</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/amaker/beans/User.hbm.xml</value>
</list>
</property>
</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>  <form-beans >
    <form-bean name="userForm" type="com.amaker.form.UserForm"  />  </form-beans>  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      input="/login.jsp"
      name="userForm"
      path="/user"
      scope="request"
      parameter="action"
      type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="success" path="/Success.jsp"></forward>
</action>
  </action-mappings> <message-resources parameter="com.amaker.properties.ApplicationResources" />
  
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> 
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/> 
</plug-in> </struts-config>

解决方案 »

  1.   


    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> 
    <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/> 
    </plug-in> 
    楼主也没说明出了什么问题 
    别人回答还要帮你搭好环境
    提问的智慧......我猜应该是applicationContext.xml找不到吧
    默认保存在/WEB-INF/classes/applicationContext.xml、
    classpath*:applicationContext.xml瞎猜的 不对也不要抨击啊