SSH框架之Structs2

1,控制层框架 与 springmvc 效果相同
作用:  数据处理    调用后台代码    产生响应     异常处理    国际化
2,核心控制器
    1. springMVC  ---> DispatcherServlet
    2. Struts2    ---> 2.1.3之前版本       FilterDispatcher
    3. 2.1.3开始           StrutsPrepareAndExecuteFilter
3,配置核心文件 web.xml
<filter>
          <filter-name>struts2</filter-name>
          <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
     <filter-mapping>
          <filter-name>struts2</filter-name>
          <url-pattern>/*</url-pattern>
     </filter-mapping>
4,启动Spring
<!-- 启动spring -->
     <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     <!-- 如果applicationContext.xml是放在src下的 必须是/WEB-INF/classes/applicationContext.xml -->
     <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
     </context-param>
     <!-- 启动spring -->
5,创建Action三种方式
普通java类 属性注入 POJO
public class LoginAction {
     //属性注入
     private String username;
     private String password;
     
     public String execute() {
          
          System.out.println(username+"\t"+password);
          
          if("admin".equals(username)&&"123".equals(password)){
              //request.setAttribute("name",username);
              return "success";
          }else{
              return "error";
          }
          
     }
     
     public String getUsername() {
          return username;
     }
     public void setUsername(String username) {
          this.username = username;
     }
     public String getPassword() {
          return password;
     }
     public void setPassword(String password) {
          this.password = password;
     }
     
}
 
实现 action/模型驱动
public class LoginAction2 implements Action,ModelDriven<User>{
     private User user = new User();
     
     public String execute() {
          System.out.println(user.getUsername() + "\t" + user.getPassword());
          return "success";
     }
     public User getModel() {
          return user;
     }
}
 
继承 actionSupport
 
public class LoginAction3 extends ActionSupport {
     private User user;
     public User getUser() {
          return user;
     }
     public void setUser(User user) {
          this.user = user;
     }
     @Override
     public String execute() throws Exception {
          System.out.println(user.getUsername() + "\t" + user.getPassword());
          return SUCCESS;
     }
}
6,跳转方式
 
默认跳转为请求转发
重定向方式的跳转配置
 
posted @ 2018-10-29 15:51  Jacky森  阅读(153)  评论(0编辑  收藏  举报