刚刚进公司来~~~在学校的时候学习了Spring跟Hibernate、自己做过用Spring跟Hibernate+Servlet来开发项目。现在用Struts2了不知道还应不应该加上Servlet。不加Servlet怎么使用Struts2的Action来处理回传值!
还有希望高手能够解释下Spring的依赖注入跟IOC、会用但是不知道原理很纠结~~~~还有项目的架构也顺便问问了!
我的项目架构如下:按照的是面向接口编程的思想!Action是交给Spring管理的。Hibernate的SessFactory跟事务都是用Spring管理。
当然还有许许多多的疑惑~~但是不好意思说太多! 真是越学越觉得自己什么也没学到啊!
希望无论老手、新手、还是黑手都来看看说说~~~给点意见!!!

解决方案 »

  1.   

    在struts.xml文件里面配置Action
    <action name="访问Action的名字" class="Action路径">
      <result name="success">XX.jsp</result> 
      <result name="error">XX.jsp</result> 
     </actio>
      

  2.   

    1、不用servlet,如果想在访问之前添加一些共同处理,可以在拦截器里处理。
    2、Struts2会直接将页面的请求值(request对象)注入到action的属性中,当然必须有get、set方法。所以action就是一个pojo。
      

  3.   

    ssh整合后,可是声明所有的bean有spring来管理,在web.xml需要加入 <listener>
           <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的模块来实现,具体的你看看书就知道了
      

  4.   

    struts的action大部分情况下可以替代servlet了struts2提供解耦和非解耦的方式访问servlet API
      

  5.   

       大概知道用ActionContext来存值、现在搞不懂的就是为什么要用Spring来管理Struts啊!还有就是Spring自己管理Session的打开关闭的配置~~~嘿嘿!  谢谢!
      

  6.   

    spring就是处理事务,和和创建对象,
    struts2 里用action implements ServletRequestAware,ServletContextAware 来得到HttpServletRequest,ServletContext application 对象,
    还有不servlet的request,session 对象,implements RequestAware,SessionAware,ApplicationAware来得到得到Map 类型的request,session 对象
      

  7.   

    struts的action大部分情况下可以替代servlet了
      

  8.   

    嗯~~~很感谢大大们帮助、但还是实际操作来得爽!刚刚做了个拦截器、但是里面不理解的东西很多!我在这里列出来希望大大们在给看看~~~俺是90后的、出来混真的太不容易了!好怀恋学校的生活啊!好、看代码:
    我的拦截器类的代码: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>
    上面配置文件逻辑名是否跟哪个哪个有联系什么的。
    暂时就这么多了~~~希望各位能给看看、给指点指点!!!
      

  9.   

    、<interceptor-ref name="defaultStack" />默认的拦截器,如何你定义了你的拦截器,就必须在你的拦截器后面加上。
    Action的属性和配置文件的name对应如 :
    返回Action.ERROR对应<result name="error">/login.jsp</result>
      

  10.   

    struts2的核心就是用action代替servlet来处理业务,处理完业务后的页面跳转时通过xwork来做的。如果是ajax回传值的话就需要通过用流来处理 和jsp的out对象一样
      

  11.   

    个人认为:
    struts2取代了servlet你把以前访问servlet的url换成action就可以了如果需要在 action中访问web对象的话,
    可以使用
    ServletActionContext.getRequet();
    ServletActionContext.getResponse();
      

  12.   

         哦~~~原来是这样的啊!对了今天碰到一个问题、就是很多资源上说的Struts2要加载的几个包。先说是5个、后面今天俺报错了、在查看、又是要7个!
          不知道哪位知道哪个包哪个包是干什么用的吗?
     
      

  13.   

    汗啊~~~差点忘记AJAX了!  只是在学校用DWR玩过现在公司要求用JQuery来搞、哎!晕了晕了!
       对了不知道有没有人碰到action的实例错误的情况! 比如、我在页面上第一次故意输入错的值给Action类来处理、返回页面后、也确实是可以把错误信息显示出来、但是第二次就算输入正确也没有反应了!
     后来把Spring中注入的Action bean由单例模式改成一个Action一个实例的模式、这个问题貌似解决了!但是还是很模糊、不知道在错误很多的情况会不会处理好!
      看来这就基础不是一般的差了! 哎~~~得狠狠补补了!
      

  14.   

    恩、谢谢了~ 经过一天的学习、已经基本上清楚怎样把request和response从Servlet转到Struts2的Action来处理了! 相信多多练习就OK了! 很想对每个人都回一张贴、这样下次有问题问你们的时候相信只要你们有时间都会给回答的!呵呵!!!