在servlet类
如何使用ssh架构访问数据库
保存servlet中的数据
2条回答 默认 最新
- samungtered 2009-08-05 17:07关注
[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
然后就可以操作数据库了本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
- ¥15 活动选择题。最多可以参加几个项目?
- ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
- ¥15 vs2019中数据导出问题
- ¥20 云服务Linux系统TCP-MSS值修改?
- ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
- ¥20 怎么在stm32门禁成品上增加查询记录功能
- ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
- ¥50 NT4.0系统 STOP:0X0000007B
- ¥15 想问一下stata17中这段代码哪里有问题呀