Struts2基礎總結

一、概述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="提交"/>&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>

 

 三、異常機制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>

 

 四、小結

相關文章
相關標籤/搜索