一、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>

最新文章

  1. python 03
  2. 自定义BadgeView
  3. L20n – Mozilla 推出的 Web 本地化框架
  4. JAVA多线程经典范列:生产者与消费者
  5. C# Enum Type
  6. 菜鸟学习Spring——60s让你学会动态代理原理
  7. html基础之 input:type
  8. Javascript学习十
  9. TCP粘包、拆包及解决
  10. 跟我学ASP.NET MVC之四:使用Razor
  11. bzoj3435 [Wc2014]紫荆花之恋
  12. springboot 学习之路 9 (项目启动后就执行特定方法)
  13. 图片和base64互转
  14. The Little Prince-12/05
  15. POJ 3693 Maximum repetition substring(连续重复子串)
  16. Python 3 利用 Dlib 19.7 进行人脸检测
  17. MySQL数据库事务各隔离级别加锁情况--read committed &amp;&amp; MVCC(转)
  18. C语言函数參数传递原理
  19. 企业搜索引擎开发之连接器connector(十八)
  20. day 05 万恶之源-基本数据类型(dict)

热门文章

  1. POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法
  2. Codeforces Round #283 (Div. 2) C. Removing Columns 暴力
  3. saga中的saga(A Saga on Sagas)
  4. Java基础加强总结(二)——泛型
  5. Bootstrap 3之美01-下载并引入页面
  6. 使用docker exec命令
  7. Google Admob广告Android全攻略1
  8. ExtJS创建选项卡
  9. FFmpeg深入分析之零-基础
  10. arm指令版本