刚刚进公司来~~~在学校的时候学习了Spring跟Hibernate、自己做过用Spring跟Hibernate+Servlet来开发项目。现在用Struts2了不知道还应不应该加上Servlet。不加Servlet怎么使用Struts2的Action来处理回传值!
还有希望高手能够解释下Spring的依赖注入跟IOC、会用但是不知道原理很纠结~~~~还有项目的架构也顺便问问了!
我的项目架构如下:按照的是面向接口编程的思想!Action是交给Spring管理的。Hibernate的SessFactory跟事务都是用Spring管理。
当然还有许许多多的疑惑~~但是不好意思说太多! 真是越学越觉得自己什么也没学到啊!
希望无论老手、新手、还是黑手都来看看说说~~~给点意见!!!
还有希望高手能够解释下Spring的依赖注入跟IOC、会用但是不知道原理很纠结~~~~还有项目的架构也顺便问问了!
我的项目架构如下:按照的是面向接口编程的思想!Action是交给Spring管理的。Hibernate的SessFactory跟事务都是用Spring管理。
当然还有许许多多的疑惑~~但是不好意思说太多! 真是越学越觉得自己什么也没学到啊!
希望无论老手、新手、还是黑手都来看看说说~~~给点意见!!!
<action name="访问Action的名字" class="Action路径">
<result name="success">XX.jsp</result>
<result name="error">XX.jsp</result>
</actio>
2、Struts2会直接将页面的请求值(request对象)注入到action的属性中,当然必须有get、set方法。所以action就是一个pojo。
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>这个监听器,struts2可以自动处理request中的参数与action对应(通过过滤器),
返回则通过<action name="访问Action的名字" class="actionId">
<result name="success">XX.jsp</result>
<result name="error">XX.jsp</result>
</actio>
注意actionId是spring配置文件中声明的 id,spring对hibernate进行了封装,用起来很方便,单一些特殊的功能不能实现或实现很麻烦时,可以调用hibernate的模块来实现,具体的你看看书就知道了
struts2 里用action implements ServletRequestAware,ServletContextAware 来得到HttpServletRequest,ServletContext application 对象,
还有不servlet的request,session 对象,implements RequestAware,SessionAware,ApplicationAware来得到得到Map 类型的request,session 对象
我的拦截器类的代码:public class InterceptTest extends AbstractInterceptor { @Override
public String intercept(ActionInvocation hzw) throws Exception {
String userName = (String)hzw.getInvocationContext().get("username");
if (null != userName && "".equals(userName)) {
System.out.println("拦截器:合法用户登录---");
return hzw.invoke();
}else {
System.out.println("拦截器:用户未登录---");
return Action.ERROR;
}
}
}
Action类代码:public class LogInAction extends ActionSupport {
private IEmpBiz empBiz;
public void setEmpBiz(IEmpBiz empBiz) {
this.empBiz = empBiz;
}
public String username;
public String userpass;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
public String execute(){
List list = empBiz.getAll("from Emp mp where mp.ename=?", new Object[] {this.username});
if(null == list || list.size() <= 0){
super.addFieldError("username", "用户名或密码错误!");
return ERROR;
}
if (null!=list && list.size()>0) {
List<Emp> li = empBiz.getAll("from Emp", null);
ActionContext.getContext().put("list", li); //表示存放在Request范围中
}
return SUCCESS;
}
}Struts2配置文件:<!-- 设置Struts2的编码方式 -->
<constant name="struts.i18n.encoding" value="gbk" />
<package name="struts2" extends="struts-default"> <!-- 定义权限控制拦截器 -->
<interceptors>
<interceptor name="hzwInt" class="com.boxun.intercept.InterceptTest">
</interceptor>
</interceptors> <!-- 定义全局处理结果 -->
<global-results>
<!-- 逻辑名为LOGIN的结果,映射到/login.jsp页面 -->
<result name="LOGIN">/login.jsp</result>
</global-results> <!--
将Action交给Spring管理 只要在Spring文件中注入一个bean把Action的class路径写上去、把配在Spring
bean里面的类id取回来当现在的class就行了
-->
<action name="login" class="loginBiz">
<result name="success">/index.jsp</result>
<result name="input">/login.jsp</result>
<result name="error">/login.jsp</result>
<!-- 使用拦截器 -->
<interceptor-ref name="defaultStack" />
<interceptor-ref name="hzwInt" />
</action>
</package>
在上面的程序中有几点为明白:
1、在拦截器类里面:Action.ERROR(或者是LOGIN啊什么的)这些个属性的意思、还有就是我想要它直接不拦截了、直接去Action里面算了、但是Action....不知道要选择哪个。
2、<interceptor-ref name="defaultStack" />这个好像是必须的、这是什么东西?
3、<!-- 定义全局处理结果 -->
<!-- 定义全局处理结果 -->
<global-results>
<!-- 逻辑名为LOGIN的结果,映射到/login.jsp页面 -->
<result name="LOGIN">/login.jsp</result>
</global-results>
上面配置文件逻辑名是否跟哪个哪个有联系什么的。
暂时就这么多了~~~希望各位能给看看、给指点指点!!!
Action的属性和配置文件的name对应如 :
返回Action.ERROR对应<result name="error">/login.jsp</result>
struts2取代了servlet你把以前访问servlet的url换成action就可以了如果需要在 action中访问web对象的话,
可以使用
ServletActionContext.getRequet();
ServletActionContext.getResponse();
不知道哪位知道哪个包哪个包是干什么用的吗?
对了不知道有没有人碰到action的实例错误的情况! 比如、我在页面上第一次故意输入错的值给Action类来处理、返回页面后、也确实是可以把错误信息显示出来、但是第二次就算输入正确也没有反应了!
后来把Spring中注入的Action bean由单例模式改成一个Action一个实例的模式、这个问题貌似解决了!但是还是很模糊、不知道在错误很多的情况会不会处理好!
看来这就基础不是一般的差了! 哎~~~得狠狠补补了!