Struts2(七)基础小结

一、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>
原文地址:https://www.cnblogs.com/liunanjava/p/4375908.html