本文共 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/