Struts2 属性驱动、模型驱动、异常机制
模型驱动使用单独的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<UserBean>{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: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">5: <html>6: <head>7: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">8: <title><s:text name="login.title" /></title>
9: </head>10: <body>11: <s:form action="login" align="center">12: <s:textfield name="username" key="user"/>13: <s:password name="password" key="pass"/>14: <s:submit value="登录" key="login"/>15: </s:form>16: </body>17: </html>
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: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">5: <html>6: <head>7: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">8: <title>成功界面</title>9: </head>10: <body>11: <s:property value="model.tip"/>
12: </body>13: </html>
二、异常处理
MVC框架异常处理的流程:
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: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">5: <html>6: <head>7: <title><s:text name="login.title" /></title>
8: </head>9: <body>10: <s:form action="login" align="center">11: <s:textfield name="username" key="user"/>12: <s:password name="password" key="pass"/>13: <s:submit value="登录" key="login"/>14: </s:form>15: </body>16: </html>
4、struts.xml配置如下:
1: <?xml version="1.0" encoding="UTF-8" ?>2: <!DOCTYPE struts PUBLIC3: "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4: "http://struts.apache.org/dtds/struts-2.3.dtd">
5: <struts>6: <global-results>7: <result name="sql">/exception.jsp</result>
8: <result name="root">/exception.jsp</result>
9: </global-results>10: <global-exception-mappings>11: <exception-mapping exception="SQLException" result="sql"/>12: <exception-mapping exception="Exception" result="root"/>13: </global-exception-mappings>14: <action name="login" class="cc.openhome.LoginAction">15: <result name="success">/success.jsp</result>
16: <result name="error">/error.jsp</result>
17: <result name="input">/denglu.jsp</result>
18: </action>19: </struts>
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: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">5: <html>6: <head>7: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">8: <title>异常界面</title>9: </head>10: <body>11: 出现的问题是:<s:property value="exception.message"/><br>
12: <s:property value="exception"/>
13: <!--<s:property value="exception"/>-->
14: </body>15: </html>
6、运行结果
输出异常信息有两种方式:
<s:property value=”exception” />输出异常对象本身
<s:property value=”exceptionStack” />输出异常堆栈信息
当神已无能为力,那便是魔渡众生