当前位置: 代码迷 >> Java Web开发 >> SSH项目,applicationcontext.xml加载问题
  详细解决方案

SSH项目,applicationcontext.xml加载问题

热度:787   发布时间:2011-11-18 15:01:41.0
SSH项目,applicationcontext.xml加载问题
public class Login1Action extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        Login1Form login1Form = (Login1Form) form;
        ActionForward forward = mapping.getInputForward();
        String userName = login1Form.getUserName();
        String password = login1Form.getPassword();
        ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
        UserService service = (UserService)ctx.getBean("UserService");
        User userFromDB = service.getUserByUserName(userName);
        if(userFromDB!=null){
             if(password.equals(userFromDB.getPassword()))
            forward = mapping.findForward("success");
             else
            forward = mapping.findForward("fail");
           }
        return forward;

    }
}

这是登录的action。如何在action中更好使用getBean();
而不是使用前再弄一个ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
搜索更多相关主题的帖子: 项目  password  request  forward  public  

----------------解决方案--------------------------------------------------------
回复 楼主 JeffLi
今天换了个思路
写一单例来加载一次配置文件

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppContext {
    private static ApplicationContext appContext = null;

    private AppContext() {

    }

    public static synchronized ApplicationContext getAppContext() {
        if (appContext == null) {
            appContext = new ClassPathXmlApplicationContext(
                    new String[] { "applicationContext.xml" });
        }
        return appContext;
    }
}


然后applicationContext.xml中配置
    <bean id="AppContext" class="com.jeff.persist.AppContext"/>
    <bean id="LoginAction" class="com.jeff.struts.action.LoginAction">
    <property name="appContext">
    <ref bean="AppContext"/>
    </property>
    </bean>

Action改成:

public class LoginAction extends Action {
    private ApplicationContext appContext;

    public void setAppContext(ApplicationContext appContext) {
        this.appContext = appContext;
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        LoginForm loginForm = (LoginForm) form;
        ActionForward forward = mapping.getInputForward();
        String userName = loginForm.getUsername();
        String password = loginForm.getPassword();
        UserService userService = (UserService) appContext
                .getBean("UserService");
        User user = userService.getUserByUserName(userName);
        System.out.println(userName + password);
        if (user != null) {
            if (password.equals(user.getPassword())) {
                forward = mapping.findForward("success");
            } else {
                forward = mapping.findForward("fail");
            }
        }
        return forward;
    }
}

没提示出错.但是启动Tomcat时报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'LoginAction' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [com.jeff.persist.AppContext] to required type [org.springframework.context.ApplicationContext] for property 'appContext'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [com.jeff.persist.AppContext] to required type [org.springframework.context.ApplicationContext] for property 'appContext': no matching editors or conversion strategy found

这种出错.spring不能往Servlet文件中注入.还是不能注入spring的ApplicationContext.xml的加载类...


哎..还是困在怎么加载ApplicationContext.xml文件.和使用getBean()方法使用上...求高手指点迷津啊..

----------------解决方案--------------------------------------------------------
回复 楼主 JeffLi
public class LoginAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        LoginForm loginForm = (LoginForm) form;
        ActionForward forward = mapping.getInputForward();
        String userName = loginForm.getUsername();
        String password = loginForm.getPassword();
        WebApplicationContext ctx = WebApplicationContextUtils
                .getWebApplicationContext(this.getServlet().getServletContext());
        UserService userService = (UserService) ctx.getBean("UserService");
        User user = userService.getUserByUserName(userName);
        System.out.println(userName + password);
        if (user != null) {
            if (password.equals(user.getPassword())) {
                forward = mapping.findForward("success");
            } else {
                forward = mapping.findForward("fail");
            }
        }
        return forward;
    }
}

哈哈.终于搞定了...用这个WebApplicationContext感觉好多了.
----------------解决方案--------------------------------------------------------
顶起来
----------------解决方案--------------------------------------------------------
在web.xml文件中加入监听,这样在web开发中就可以使用了,在服务器启动时,会加载spring上下文
----------------解决方案--------------------------------------------------------
  相关解决方案