Struts2 (7) basic summary and struts2 basic summary
I. struts2 and action
Ii. Result
Iii. struts. xml
Iv. namespace
First absolute path
<form action="${pageContext.request.contextPath }/user/login.action" method="post">
Second
<form action="<%=request.getContextPath() %>/user/login.action" method="post">
On the third page, directly write the following code:
<% 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">
Do not change the submitted address
V. Exception Mechanism
Local exception
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 ("perform the add operation! "); If (1 = 1) {// call the service method throw new InputMismatchException ();} return" success ";} public String update () throws NullPointerException {System. out. println ("perform the update operation! "); If (1 = 1) {// call the service method throw new NullPointerException ();} return" success ";} public String delete () throws SQLException {System. out. println ("delete operation! "); If (1 = 1) {// call the service method throw new SQLException ();} return" success ";}}
Page
<% @ 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">
Error Page
<%@ 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">
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>
Modify struts. xml due to a global exception
<? 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 "> </Ti On-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> <! -- Inherit base package --> <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>