2019独角兽企业重金招聘Python工程师标准>>>

1、概述

2、namespace问题

1)不断追加原因:相对路径(页面action中“user/login”)

2)不报错:

login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%String contextPath = request.getContextPath();out.print("contextPath is " + contextPath);
%>
<!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="<%=contextPath%>/user/login" method="post">用户名:<input type="text" name="userName"/><br/>密码:<input type="password" name="password"/><br/>年龄:<input type="text" name="age"/><br/><input type="submit" value="提交"/>&nbsp;&nbsp;<input type="reset" value="重置"/></form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%String contextPath = request.getContextPath();out.println("contextPath is " + contextPath);String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + contextPath + "/";out.println("<br/> basePath is " + basePath);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><base href="<%=basePath%>"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title>
</head>
<body><%-- <form action="<%=contextPath%>/user/login" method="post"> --%><form action="user/login" method="post">用户名:<input type="text" name="userName"/><br/>密码:<input type="password" name="password"/><br/>年龄:<input type="text" name="age"/><br/><input type="submit" value="提交"/>&nbsp;&nbsp;<input type="reset" value="重置"/></form>
</body>
</html>

 3、异常机制

HouseAction:

package com.ljb.web.action;
import com.opensymphony.xwork2.ActionSupport;
public class HouseAction extends ActionSupport {/*** 添加房屋信息* @return*/public String add () throws NullPointerException{System.out.println("处理添加房屋信息。");// 模拟添加房屋信息异常/*try {* 调用service方法if (1==1) {throw new Exception();}} catch (Exception e) {return ERROR;}*/if (1==1) {throw new NullPointerException();}return SUCCESS;}/*** 修改房屋信息* @return*/public String update () {System.out.println("处理修改房屋信息。");return SUCCESS;}@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubreturn SUCCESS;}
}

注:只声明不捕获,在struts.xml中处理并跳转到error.jsp页面,给出异常信息

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><!-- Add packages here --><constant name="struts.devMode" value="true" /><package name="house" namespace="/house" extends="struts-default"><global-results><result name="error">/error.jsp</result></global-results><action name="house_*" class="com.ljb.web.action.HouseAction" method="{1}"><result>/house_{1}_success.jsp</result><exception-mapping result="error" exception="java.lang.NullPointerException"></exception-mapping></action></package>
</struts>

error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>
<h1>error页面</h1>
<p>异常信息:<s:property value="exception"/></p>
<p>堆栈信息:<s:property value="exceptionStack"/></p>
</body>
</html>

使用全局异常:

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><!-- Add packages here --><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.lang.NullPointerException"></exception-mapping></global-exception-mappings></package><package name="house" namespace="/house" extends="base"><!-- <global-results><result name="error">/error.jsp</result></global-results> --><action name="house_*" class="com.ljb.web.action.HouseAction" method="{1}"><result>/house_{1}_success.jsp</result><!-- <exception-mapping result="error" exception="java.lang.NullPointerException"></exception-mapping> --></action></package>
</struts>

 4、小结

转载于:https://my.oschina.net/u/2320342/blog/414373

Struts2基础总结相关推荐

  1. Struts2基础学习总结

    Struts 2是在WebWork2基础发展而来的. 注意:struts 2和struts 1在代码风格上几乎不一样. Struts 2 相比Struts 1的优点: 1.在软件设计上Struts 2 ...

  2. Struts2基础(1)_MVC

    2019独角兽企业重金招聘Python工程师标准>>> Struts2是有传统的Struts1和WebWork两个经典MVC框架发展起来,无论从Struts2的设计角度或则实际项目中 ...

  3. Struts2基础知识(三)

    本文主要包括以下内容 OGNL表达式 标签 防止表单重复提交 使用第三方插件 tomcat启动时struts2容器做的事 OGNL表达式 OGNL是Object Graphic Navigation ...

  4. Struts2基础知识(二)

    本文主要包括以下内容 文件上传,多文件上传 自定义拦截器 用户输入验证 国际化 文件上传 将头设置为enctype="multipart/form-data" <body&g ...

  5. Struts2基础知识

    本文主要包括以下内容 struts2常用常量的定义与意义 struts2处理流程 拆分struts 动态方法调用,使用通配符 接收请求参数 中文编码问题 自定义类型转化器 访问或添加request/s ...

  6. struts2基础梳理(二)

    本篇主要有:设置struts2匹配的扩展名.使用通配符,值栈,声明式异常以及标签. 设置扩展名: 默认是对.action和不加不论什么扩展名的进行处理.能够设置: <constant name= ...

  7. struts2基础----自定义拦截器

    这一章,我们开始struts2中拦截器的学习. 自定义拦截器 一.增加一个自定义的拦截器为类 package com.huhx.interceptor;import com.opensymphony. ...

  8. 最详细Struts2基础入门

    struts2 1 框架概述 1.1什么是框架 1.2 三大框架 1.3 三层架构 1.4 MVC回顾 2 Struts简介 2.1 Struts2概述 2.2 Struts2的来历 3 Struts ...

  9. struts2《轻量级框架应用与开发--S2SH》笔记

    文章目录 struts2基础 struts2处理步骤 创建一个struts2项目 struts2进阶 struts2常量配置(struts.xml) 实现action pojo实现方式 action接 ...

  10. struts2 文件上传为空

    许多朋友和我一样,取得的文件为null 属性当然也是对的.为什么呢. 是因为struts2上传时要加上传拦截, 如: (1)   <action name="file" cl ...

最新文章

  1. centos7.9使用ssh命令进行登陆云服务器
  2. boost::hana::size用法的测试程序
  3. Linux---输入与输出函数总结
  4. JavaScript 总结几个提高性能知识点
  5. [css] :placeholder-shown和:focus-within这两个伪类你有使用过吗?说说看
  6. Java程序员情人节_盘点程序员情人节的表白,前端程序员最浪漫,后端不服来战...
  7. python-运算符之算术运算符
  8. 用python画机器猫代码_如何用Python画一只机器猫?| 原力计划
  9. 多核、多处理器、并发、并行、超线程概念总结
  10. 软件项目管理案例教程 第4版 课后习题答案
  11. .so文件的生成和使用
  12. WindowsServer实训项目——第一章:安装与激活WindowsServer
  13. Kotlin For循环详解
  14. ping不通 之 关闭防火墙
  15. ABP AOP 用例
  16. R 绘制带有数字标签的多分类柱状图
  17. html中如何淡化背景图片,excel怎么去除背景图片
  18. 基于 CentOS 搭建微信小程序服务
  19. Linux安装Kibana详细教程
  20. python 单词纠错_用 Python 实现英文单词纠错功能

热门文章

  1. C#相关控件使用总结
  2. 如何下载行政区划数据
  3. 自己创建一个本地服务器,实现文件下载
  4. java after 函数_函数周期表丨信息丨值丨ISONORAFTER
  5. Bootstrap前端框架
  6. UnsupportedOperationException:can‘t convert to dimension :typx=0x1
  7. Flink Next:Beyond Stream Processing
  8. Flink on Zeppelin (2) - Batch 篇
  9. Android FrameWork浅识
  10. python实现生日祝福短信_Python实现好友生日提醒