
dao里创建一个存储数据库查询出来List泛型集合,action调用这个集合
Map request = (Map) ActionContextgetContext()get("request");
requestput("userlist", UserDaogetAllUser());
或是
<c:foreach items="${requestScopeuserlist}" var="user">
${user。。。。}
</c:foreach>
<bean id="springBean" scope="prototype" class=""<property name="name" value="chen"/</bean<bean id="myAction" scope="prototype" class=""<property name="springBean" ref="springBean"/</bean如果是j2ee应用,启动web应用时将会自动加载ApplicationContext实例(Spring容器负责创建Bean实例)一旦struts2的myAction实例化,其中的SpringBean也会被自动注入进来,从而达到使用SpringBean的目的。[问题]但是仍有需要通过代码来调用SpringBean的情况:1)对于不是由spring创建管理的类,如在java 代码中直接使用new去创建一个对象,并且想在这个对象中使用SpringBean;因为这个对象并不是由Spring容器创建管理的类,所以即使它有setter方法,容器的springBean也不会被注入。2)动态更改springBean中的属性值,如在代码运行时,name值需要发生变动;3)对于一个独立的应用程序[解决]定义一个非Spring容器创建管理的类public class NonSpringClass implements ServletContextAware {private SpringBean springBean;//如果 testGetBean不是被Spring容器创建管理,即使它有setter方法,容器的springBean也不会被注入。public void setSpringBean(SpringBean springBean){thisspringBean=springBean;}//利用ApplicationContext 从spring容器中获得springBean;//Spring有两个核心接口BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口,//它们代表了Spring容器,Spring容器是产生Bean的工厂,用于管理容器中的Bean。
举个例子你就好明白了。\x0d\例如,首先,你写了一个ExampleBean里面有属性userName和password都是String类型,然后别忘了写他们的setter和getter方法,publicvoidsetUserName(StringuserName)等等。\x0d\然后,写Action,在Action类中加入成员变量privateStringuName;privateStringuPwd;\x0d\一定要再写上他们的Setter和Getter方法。publicvoidsetUName(Strings)等等。\x0d\然后你在execute方法中创建一个bean对象。如:\x0d\ExampleBeanb=newExampleBean(thisgetUName(),thisgetUPwd());\x0d\就可以创建这个bean对象了。\x0d\\x0d\具体传值流程是:首先你访问jsp页面,然后比如jsp页面上有一个登录框,你可以输入用户密码。\x0d\\x0d\\x0d\\x0d\\x0d\\x0d\这里注意的是标签中name属性对应的就是Action里面的值name叫uName,提交表单后在Action里就会自动调用setUName("你输入的值");然后就执行execute方法,你就可以创建bean了。\x0d\明白过程了吗?如果不会还可以追问。
只要搜一下“获取 spring bean”,获取spring bean的N中方法都出来了。线程中获取和普通类中获取方法是一样的。
下面是一种方法。这个类需要配置在Spring中。
使用的时候直接:Bean bean = SpringUtilgetBean("bean的id", Beanclass);//Beanclass是bean的类对象,比如获取 UserService的bean,就是:
1
UserService us = SpringUtilgetBean("userService", UserServiceclass);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import orgspringframeworkbeansBeansException;
import orgspringframeworkcontextApplicationContext;
import orgspringframeworkcontextApplicationContextAware;
/
Spring IOC上下文工具类
@ClassName: SpringUtil
@Description:
@author
@date 2014-6-21 下午02:06:48
/
public class SpringUtil implements ApplicationContextAware {
/
当前IOC
/
private static ApplicationContext applicationContext;
/
设置当前上下文环境,此方法由spring自动装配
/
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
applicationContext = arg0;
}
/
从当前IOC获取bean
@param id
bean的id
@return
/
public static <T> T getBean(String id, Class<T> requiredType) {
T t = applicationContextgetBean(id, requiredType);
return t;
}
}
两个类型的serialVersionUID不一致,远端序列化对象类型的serialVersionUID为-5723701046347946317,而你本地JVM中该类型的serialVersionUID为-798217051365457021。修改你本地的代码将该类型UID与远端类型的UID相同。
1jsp页面如果想要根据id直接查询信息的话,可能会需要这样的代码
2而应用类Spring框架之后如上图的NewsService里面是没有实例化过的NewsDao的,这样上面图中的方法就执行不了
3那假如想要使用NewsServcie中的方法,就需要去找Spring,在Action因为设置了setter方法注入所以可以直接获得实例化好的对象,那在jsp中呢?
4首先你需要有一个jar包,形如spring-web-320M2jar,将此包加入build Path并部署或者直接复制到WEB-INF/lib下,这是spring应用在web项目时需要用到的jar包
然后在jsp页面中导入相关的工具类:
<%@ page import="orgspringframeworkwebcontextsupportWebApplicationContextUtils"%><%@ page import="orgspringframeworkwebcontextWebApplicationContext"%>
5最后通过以下语句获取配置文件中相应的Bean
WebApplicationContext wac = WebApplicationContextUtilsgetRequiredWebApplicationContext(thisgetServletContext()); NewsService service = (NewsService)wacgetBean("newsService");
注意getBean()方法中传入的是配置文件中的Bean的id
这样就可以在页面中访问Spring的Bean了,同时也可以访问service的方法了
params 可以是字符串也可以是Map格式的数据。
在action获取参数:
比如:params="name=hh&password=123";
那么在action中 私有属性name 和password 可以获取值(struts2&&属性必须要有get set 函数)
比如:var user={name:'hh',password:'123'};
params={user:user};
这样可以在action中用user类来接受数据 user属性必须有getset函数。
以上就是关于Java项目,bean类,dao包,service包,action包,现在想用action调用数据库中数据,数据在bean里有get set全部的内容,包括:Java项目,bean类,dao包,service包,action包,现在想用action调用数据库中数据,数据在bean里有get set、如何通过代码直接获得Spring容器中的Bean、struts2的action怎么获取jsp页面的表单值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)