一、概述html
二、namespace问题java
1)不断追加缘由:相对路径(页面action中“user/login”)web
2)不报错:apache
login.jsp:app
<%@ 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="提交"/> <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="提交"/> <input type="reset" value="重置"/> </form> </body> </html>
三、异常机制jsp
HouseAction:ide
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; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } }
注:只声明不捕获,在struts.xml中处理并跳转到error.jsp页面,给出异常信息post
struts.xml:ui
<?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:spa
<%@ 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>
四、小结