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