在servlet类
如何使用ssh架构访问数据库
保存servlet中的数据
[quote]
都SSH了还用啥Servlet?不太明白lz的意思。
SSH里面一般是前台传过来一个form,form里面有dto,dto里面包含需要持久化的数据。Action将dto传给service,service里面调用dao,dao里面再使用hibernate来将dto里面的数据持久化到数据库。一层一层的。
[/quote]
还是有些情况需要用到servlet的
给个方案吧?
先自己重写一个ContextLoaderListener ,然后配置在web.xml替代ContextLoaderListener
[code="java"]
public class SpringLoaderListener extends ContextLoaderListener {
@Override
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
ServletContext context = event.getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
SpringContextUtil.setApplicationContext(ctx);
}
}
[/code]
后面就是一个操作SpringContextUtil
[code="java"]
public class SpringContextUtil {
private static ApplicationContext context;
public static void setApplicationContext(ApplicationContext acx) {
context = acx;
}
public static ApplicationContext getApplicationContext() {
return context;
}
public static Object getBean(String name) throws BeansException {
return context.getBean(name);
}
public static Object getBean(String name, Class requiredType) throws BeansException {
return context.getBean(name, requiredType);
}
public static boolean containsBean(String name) {
return context.containsBean(name);
}
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return context.isSingleton(name);
}
public static Class getType(String name) throws NoSuchBeanDefinitionException {
return context.getType(name);
}
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return context.getAliases(name);
}
}
[/code]
你可以在servlet里通过该Utils工具方法可以得到你想要的bean,dao或者service
然后就可以操作数据库了
都SSH了还用啥Servlet?不太明白lz的意思。
SSH里面一般是前台传过来一个form,form里面有dto,dto里面包含需要持久化的数据。Action将dto传给service,service里面调用dao,dao里面再使用hibernate来将dto里面的数据持久化到数据库。一层一层的。