Struts2(七)基础小结
一、struts2和action

二、Result

三、struts.xml

四、namespace



第一种绝对路径
<form action="${pageContext.request.contextPath }/user/login.action" method="post">
第二种
<form action="<%=request.getContextPath() %>/user/login.action" method="post">
第三种 页面中直接写以下代码
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<form action="user/login.action" method="post">
提交地址不用改变
五、异常机制

局部异常
package com.pb.web.action; import java.sql.SQLException;
import java.util.InputMismatchException; import com.opensymphony.xwork2.ActionSupport; public class HourseAction extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 1L;
public String add() throws InputMismatchException{
System.out.println("执行添加操作!"); if(1==1){
//调用service的方法
throw new InputMismatchException();
} return "success";
}
public String update() throws NullPointerException{
System.out.println("执行更新操作!"); if(1==1){
//调用service的方法
throw new NullPointerException(); } return "success";
}
public String delete() throws SQLException{
System.out.println("执行删除操作!"); if(1==1){
//调用service的方法
throw new SQLException();
} return "success";
} }
页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="hourse_add">
<input type="submit" value="添加"/>
</form>
<form action="hourse_update">
<input type="submit" value="更新"/>
</form>
<form action="hourse_delete">
<input type="submit" value="删除"/>
</form>
</body>
</html>
error页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
error.jsp
<s:property value="exception"/>
<s:property value="exceptionStack"/>
</body>
</html>
struts.xml
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<action name="hourse_add" class="com.pb.web.action.HourseAction" method="add">
<result name="success" type="dispatcher">
/loginSuccess.jsp
</result>
<exception-mapping result="error" exception="java.util.InputMismatchException"></exception-mapping>
</action>
<action name="hourse_update" class="com.pb.web.action.HourseAction" method="update">
<result name="success" type="dispatcher">
/loginSuccess.jsp
</result>
<exception-mapping result="error" exception="java.lang.NullPointerException"></exception-mapping>
</action>
<action name="hourse_delete" class="com.pb.web.action.HourseAction" method="delete">
<result name="success" type="dispatcher">
/loginSuccess.jsp
</result>
<exception-mapping result="error" exception="java.sql.SQLException"></exception-mapping>
</action>
全局异常更改struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results>
<result name="error">/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings> <action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package> <include file="example.xml"/> --> <!-- Add packages here -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="base" namespace="/base" extends="struts-default">
<global-results>
<result name="error">error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.util.InputMismatchException"></exception-mapping>
<exception-mapping result="error" exception="java.lang.NullPointerException"></exception-mapping>
<exception-mapping result="error" exception="java.sql.SQLException"></exception-mapping>
</global-exception-mappings> </package>
<!-- 继承base包-->
<package name="user" extends="base">
<action name="login" class="com.pb.web.action.LoginAction" method="login">
<result name="success" type="dispatcher">
/loginSuccess.jsp
<!-- http://www.baidu.com/ -->
</result>
<result name="input" type="dispatcher">
/login.jsp
</result>
</action>
<action name="hourse_add" class="com.pb.web.action.HourseAction" method="add">
<result name="success" type="dispatcher">
/loginSuccess.jsp
</result> </action>
<action name="hourse_update" class="com.pb.web.action.HourseAction" method="update">
<result name="success" type="dispatcher">
/loginSuccess.jsp
</result> </action>
<action name="hourse_delete" class="com.pb.web.action.HourseAction" method="delete">
<result name="success" type="dispatcher">
/loginSuccess.jsp
</result> </action>
</package>
</struts>
Struts2(七)基础小结的更多相关文章
- Struts2框架基础
Struts2框架基础 1.Java的框架 1.1.框架简介 在大型项目开发过程中,经常会使用到一些框架,这样做好的好处是能够提高工作效率,在java中最常用的的框架就是SSH,这其实是三个框架的简称 ...
- Struts2开发基础
Struts2开发基础 struts2采用拦截器的机制来处理用户的请求,使得业务逻辑控制器能够与ServletAPI完全脱离开. 1. Hello World! 配置web.xml <?xml ...
- Java 基础--小结
Java 基础--小结 java基础 Java源程序(.java文件)——>java字节码文件(.class文件)——>由解释执行器(java.exe)将字节码文件加载到java虚拟机( ...
- android基础小结
(注:此小结文档在全屏模式下观看效果最佳) 2016年3月1日,正式开始了我的android学习之路. 最最开始的,当然是学习怎样搭载环境了,然而苦逼的我在win10各种坑爹的指引下还是安装了一个星期 ...
- Struts2命令空间小结
sturts2命名空间小结,以tomcat为服务器 1. 命名空间配置为“/” <package name="default" namespace="/" ...
- Struts2的基础知识
Struts2属于MVC框架 Struts2的优点: 1.侵入性低 2.提供了拦截器,可以利用拦截器进行AOP编程 3.提供了类型转换器 4.支持多种表示层技术:jsp,freeMarker,Vele ...
- Struts2框架基础概念总结
一.struts2框架 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的 ...
- day--42 前端基础小结
前端基础总结 一:前端实现的原理: 小实例: 01:第一步:创建一个socket服务端: import socket server=socket.socket() ip_port=("127 ...
- Kerberos原理和基础小结
此篇文章仅做Kerberos的基本原理和基本使用做说明,本人对Kerberos了解有限,也是通过大量英文文档中翻译过来, 加上自己对Kerberos的理解所写,本人英文太菜,看文档看的头昏眼花若有写的 ...
随机推荐
- 使用清华大学开源软件镜像AOSP的“每月更新初始化包”更新指定版本的Android源码
参照官方教程:Tsinghua Open Source Mirror 1. 下载了repo工具 mkdir ~/bin PATH = ~/bin:$PATH curl https://storag ...
- PowerDesigner关系线显示名称
选中关联关系线,右击选择“格式”,打开如下窗口,将“Name” 选项进行勾选上即可. 参考: http://loginleft.iteye.com/blog/2400980
- [Phonegap+Sencha Touch] 移动开发18 Sencha Touch项目通过phonegap打包后的程序名字的问题
之前说过 sencha phonegap init com.pushsoft.myapp MyApp 之后打包的程序安装包apk的名字是"MyApp.apk",显示在手机桌面上的程 ...
- 达芬奇TI DVSDK之视频数据流过程分析
作者:openwince@gmail.com 博客:http://www.cnblogs.com/tinz 本文的copyright归openwince@gmail.com所有,使用GPL发布, ...
- 使用 MVC 5 的 EF6 Code First 入门 系列:建立一个EF数据模型
这是微软官方SignalR 2.0教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第一篇:建立一个E ...
- navicat for mysql中添加注释
mysql; # 这注释持续到行尾 mysql; -- 这注释持续到行尾 mysql ; mysql+ /* 这是 多行注释 */
- java通过System.getProperty获取系统属性
getProperties public static Properties getProperties() 确定当前的系统属性. 首先,如果有安全管理器,则不带参数直接调用其 checkProper ...
- Asp.Net MVC 3.0 使用Gzip压缩
前言 Gzip最早由Jean-loup Gailly和Mark Adler创建,用于Unix系统的文件压缩.我们在Linux中经常会用到后缀为.gz的文件,它们就是Gzip格式的.现今已经成为Inte ...
- JTS(Geometry)(转)
原文链接:http://blog.csdn.net/cdl2008sky/article/details/7268577 空间数据模型(1).JTS Geometry model (2).ISO Ge ...
- HTTP协议状态码详解(HTTP Status Code)(转)
原文链接:HTTP协议状态码详解(HTTP Status Code) 使用ASP.NET/PHP/JSP 或者javascript都会用到http的不同状态,一些常见的状态码为: 200 – 服务器成 ...
