博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2 属性驱动、模型驱动、异常机制
阅读量:6629 次
发布时间:2019-06-25

本文共 4700 字,大约阅读时间需要 15 分钟。

     模型驱动使用单独的VO(值对象)来封装请求参数和处理结果,属性驱动则使用Action实例来封装请求参数和处理结果。

一、使用模型驱动

     1、login.action采用模型驱动模式实现,采用模型驱动时必须提供对应的模型,该模型就是一个普通的javabean。

     UserBean.java

 
1: package cc.openhome;
2: public class UserBean {
3:   private String username;
4:   private String password;
5:   private String tip;//封装处理结果的tip属性
6:   public String getUsername() {
7:     return this.username;
8:   }
9:   public void setUsername(String username) {
10:     this.username = username;
11:   }
12:   public String getPassword() {
13:     return this.password;
14:   }
15:   public void setPassword(String password) {
16:     this.password = password;
17:   }
18:   public String getTip() {
19:     return tip;
20:   }
21:   public void setTip(String tip) {
22:     this.tip = tip;
23:   }
24: }

     2、对于采用模型驱动的Action而言,该Action必须实现ModelDriven接口。

    LoginAction.java

 
1: package cc.openhome;
2:
3: import com.opensymphony.xwork2.Action;
4: import com.opensymphony.xwork2.ModelDriven;
5: public class LoginAction  implements Action,ModelDriven
{
6:   private UserBean model;
7:
8:   public UserBean getModel() {
9:     return model;
10:   }
11:   public void setModel(UserBean model) {
12:     this.model = model;
13:   }
14:
15:   //处理用户请求的execute方法
16:   public String execute() throws Exception{
17:     if(getModel().getUsername().equals("nihao") &&
18:         getModel().getPassword().equals("nihao")){
19:       getModel().setTip("哈哈,服务器提示");
20:       return SUCCESS;
21:     }
22:     else{
23:       System.out.println("转换失败、、");
24:       return ERROR;
25:     }
26:   }
27: }
28:

    3、登录页面login.jsp

 
1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2:     pageEncoding="UTF-8"%>
3: <%@ taglib prefix="s" uri="/struts-tags" %>
4: 
5: 
6: 
7:     
8:      <s:text name="login.title" />
9: 
10: 
11:   
12:       
13:       
14:       
15:   
16: 
17: 

    3、success.jsp

 
1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2:     pageEncoding="UTF-8"%>
3: <%@ taglib prefix="s" uri="/struts-tags"%>
4: 
5: 
6:   
7:     
8:     成功界面
9:   
10:   
11:       
12:   
13: 

二、异常处理

     MVC框架异常处理的流程:

     1、LoginAction.java

 
1: package cc.openhome;
2:
3: import java.sql.SQLException;
4: import com.opensymphony.xwork2.Action;
5: import com.opensymphony.xwork2.ActionContext;
6: import com.opensymphony.xwork2.ActionSupport;
7:
8: public class LoginAction extends ActionSupport implements Action {
9:   private String username;
10:   private String password;
11:   public String getUsername() {
12:     return username;
13:   }
14:   public void setUsername(String username) {
15:     this.username = username;
16:   }
17:   public String getPassword() {
18:     return password;
19:   }
20:   public void setPassword(String password) {
21:     this.password = password;
22:   }
23:
24:   public String execute() throws Exception {
25:     // TODO Auto-generated method stub
26:     ActionContext ctx=ActionContext.getContext();
27:     if(getUsername().equalsIgnoreCase("root")){
28:       throw new MyException("自定义异常");
29:     }
30:     if(getUsername().equalsIgnoreCase("sql")){
31:       throw new SQLException("用户名不能为sql");
32:     }
33:
34:     if(getUsername().equals("nihao") && getPassword().equals("nihao")){
35:       ctx.getSession().put("user", getUsername());
36:       //ctx.put("tip", getText("succTip",new String[]{username}));
37:       return SUCCESS;
38:     }else{
39:       return ERROR;
40:     }
41:   }
42: }

    2、MyException.java

 
1: package cc.openhome;
2:
3: public class MyException extends Exception {
4:   public MyException (String eString){
5:     super(eString);
6:   }
7: }
8:

     3、登录页面login.jsp

 
1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2:     pageEncoding="UTF-8"%>
3: <%@ taglib prefix="s" uri="/struts-tags" %>
4: 
5: 
6: 
7:     <s:text name="login.title" />
8: 
9: 
10:   
11:       
12:       
13:       
14:   
15: 
16: 

    4、struts.xml配置如下:

 
1: 
2: 
3:   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4:   "http://struts.apache.org/dtds/struts-2.3.dtd">
5: 
6:     
7:       
/exception.jsp
8:       
/exception.jsp
9:     
10:     
11:       
12:       
13:     
14:         
15:       
/success.jsp
16:       
/error.jsp
17:       
/denglu.jsp
18:     
19: 

     5、exception.jsp(输出异常界面)

 
1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2:     pageEncoding="UTF-8"%>
3: <%@ taglib prefix="s" uri="/struts-tags"%>
4: 
5: 
6:   
7:     
8:     异常界面
9:   
10:   
11:       出现的问题是:
12:       
13:       
14:   
15: 

    6、运行结果

    输出异常信息有两种方式:

            <s:property value=”exception” />输出异常对象本身

            <s:property value=”exceptionStack” />输出异常堆栈信息

                                                          

当神已无能为力,那便是魔渡众生

转载地址:http://wgqpo.baihongyu.com/

你可能感兴趣的文章
【死磕jeesite源码】Jeesite配置定时任务
查看>>
程序8
查看>>
TBluetoothLEDevice.UpdateOnReconnect
查看>>
poj3517
查看>>
iphone http下载文件
查看>>
poj 1195:Mobile phones(二维树状数组,矩阵求和)
查看>>
java中的Static class
查看>>
json lib 2.4及其依赖包下载
查看>>
计算机中文核心期刊
查看>>
转:CEO, CFO, CIO, CTO, CSO是什么
查看>>
linux下vim更改注释颜色
查看>>
在SSL / https下托管SignalR
查看>>
Using JRuby with Maven
查看>>
醒醒吧少年,只用Cucumber不能帮助你BDD
查看>>
一名女程序员对iOS的想法
查看>>
西班牙现新型电费退款网络诈骗 侨胞需谨防上当
查看>>
Spring Websocket实现文本、图片、声音、文件下载及推送、接收及显示(集群模式)...
查看>>
最严新规发布 网络短视频平台该如何降低违规风险? ...
查看>>
云服务器ECS出现速度变慢 以及突然断开怎么办?
查看>>
208亿背后的“秘密”
查看>>