一、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(七)基础小结的更多相关文章

  1. Struts2框架基础

    Struts2框架基础 1.Java的框架 1.1.框架简介 在大型项目开发过程中,经常会使用到一些框架,这样做好的好处是能够提高工作效率,在java中最常用的的框架就是SSH,这其实是三个框架的简称 ...

  2. Struts2开发基础

    Struts2开发基础 struts2采用拦截器的机制来处理用户的请求,使得业务逻辑控制器能够与ServletAPI完全脱离开. 1. Hello World! 配置web.xml <?xml ...

  3. Java 基础--小结

    Java  基础--小结 java基础 Java源程序(.java文件)——>java字节码文件(.class文件)——>由解释执行器(java.exe)将字节码文件加载到java虚拟机( ...

  4. android基础小结

    (注:此小结文档在全屏模式下观看效果最佳) 2016年3月1日,正式开始了我的android学习之路. 最最开始的,当然是学习怎样搭载环境了,然而苦逼的我在win10各种坑爹的指引下还是安装了一个星期 ...

  5. Struts2命令空间小结

    sturts2命名空间小结,以tomcat为服务器 1. 命名空间配置为“/” <package name="default" namespace="/" ...

  6. Struts2的基础知识

    Struts2属于MVC框架 Struts2的优点: 1.侵入性低 2.提供了拦截器,可以利用拦截器进行AOP编程 3.提供了类型转换器 4.支持多种表示层技术:jsp,freeMarker,Vele ...

  7. Struts2框架基础概念总结

    一.struts2框架 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的 ...

  8. day--42 前端基础小结

    前端基础总结 一:前端实现的原理: 小实例: 01:第一步:创建一个socket服务端: import socket server=socket.socket() ip_port=("127 ...

  9. Kerberos原理和基础小结

    此篇文章仅做Kerberos的基本原理和基本使用做说明,本人对Kerberos了解有限,也是通过大量英文文档中翻译过来, 加上自己对Kerberos的理解所写,本人英文太菜,看文档看的头昏眼花若有写的 ...

随机推荐

  1. Properties属性操作

    package com.properties; import java.io.FileInputStream; import java.io.FileOutputStream; import java ...

  2. git命令行使用帮助

    克隆代码库 git clone git_project_url 提交代码 git commit -m 'commit messge'

  3. Apache参数的优化(转)

    按照前面提到的版本问题,Apache可以直接使用2.0版本产品线.针对Apache的优化主要是针对httpd.conf的优化,当然还有其他地方,如果特别留意的话,网上常有专家惊呼“居然这么多人忽略xx ...

  4. Dictionary GetOrAdd

    public static TValue GetOrAdd<TKey,TValue>( this Dictionary<TKey,TValue> dictionary, TKe ...

  5. DockManager如何停靠 z

    DockManager默认只能停靠在窗体上,如果想停靠在其他控件上,我们发现并没有选项可以选,可能目前大部分解决方法是新建一个用户控件文件,再在用户控件里面单独设计模块. 除了这种方法还有没有其他的呢 ...

  6. 《C++反汇编与逆向分析技术揭秘》之12——继承

    识别类和类之间的关系 在父类中声明为私有的成员,虽然子类对象无法直接访问,但是在子类对象的内存结构中,父类私有的成员数据依然存在. 在没有提供构造函数的时候,系统会尝试提供默认的构造函数: 当子类中没 ...

  7. Python的开源人脸识别库:离线识别率高达99.38%

    Python的开源人脸识别库:离线识别率高达99.38%   github源码:https://github.com/ageitgey/face_recognition#face-recognitio ...

  8. 利用c#+jquery+echarts生成统计报表(附源代码)

    背景: 因为最近项目要生成报表,经过几轮挑选,最终选择了百度的echarts作为报表基础类库.百度echarts简介请参考 http://echarts.baidu.com/ 虽然echarts功能强 ...

  9. @JVM垃圾回收机制的一些概念

    数据类型 Java虚拟机中,数据类型可以分为两类:基本数据类型和引用数据类型 .基本类型的变量保存的值就是数值本身:而引用类型的变量保存引用值."引用值"代表了某个对象的引用,而不 ...

  10. parameter server学习

    关于parameter server的学习: https://www.zybuluo.com/Dounm/note/517675 机器学习系统相比于其他系统而言,有一些自己的独特特点.例如: 迭代性: ...