| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1778 人关注过本帖
标题:SSH项目,applicationcontext.xml加载问题
只看楼主 加入收藏
JeffLi
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:39
专家分:116
注 册:2011-11-18
结帖率:0
收藏
 问题点数:0 回复次数:4 
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 
2011-11-18 15:01
JeffLi
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:39
专家分:116
注 册:2011-11-18
收藏
得分:0 
回复 楼主 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()方法使用上...求高手指点迷津啊..

我的企鹅:67567840
2011-11-19 16:12
JeffLi
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:39
专家分:116
注 册:2011-11-18
收藏
得分:0 
回复 楼主 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感觉好多了.

我的企鹅:67567840
2011-11-19 17:00
JeffLi
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:39
专家分:116
注 册:2011-11-18
收藏
得分:0 
顶起来

我的企鹅:67567840
2011-12-12 16:27
高寒
Rank: 2
等 级:论坛游民
帖 子:134
专家分:20
注 册:2005-10-25
收藏
得分:0 
在web.xml文件中加入监听,这样在web开发中就可以使用了,在服务器启动时,会加载spring上下文

2012-05-11 15:32
快速回复:SSH项目,applicationcontext.xml加载问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017700 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved