Getting started with java struts2-Exception Handling and type conversion

Source: Internet
Author: User

I. struts2 Exception Handling 1. custom local exception: <action> <exception-mapping result = "sonException" exception = "java. lang. arithmeticException "> </exception-mapping> </action> 2. custom global exception: <! -- Configure global exception handling --> <global-exception-mappings> <exception-mapping result = "sonException" exception = "java. lang. arithmeticException "> </exception-mapping> </global-exception-mappings> 3. priority of exception execution> when both global and local exceptions occur, local exceptions take precedence over global exceptions. If global exceptions are the same as local exceptions, the local exception prevails.> if the parent-child relationship is abnormal, the Child exception takes precedence. such as java. lang. arithmeticException is java. lang. the subclass of Exception. if the configuration is performed at the same time, the java. lang. arithmeticException prevails. example: <! -- Configure global exception handling --> <global-exception-mappings> <exception-mapping result = "sonException" exception = "java. lang. arithmeticException "> </exception-mapping> <exception-mapping result =" fatherException "exception =" java. lang. exception "> </exception-mapping> </global-exception-mappings> copy the Code <! -- Configure global result processing --> <global-results> <result name = "success" type = "dispatcher">/WEB-INF/success. jsp </result> <result name = "sonException" type = "dispatcher">/WEB-INF/sonException. jsp </result> <result name = "fatherException" type = "dispatcher">/WEB-INF/fatherException. jsp </result> </global-results> copy Code 2. type conversion in struts2 1. built-in type converter HTTP does not have the concept of type. Only parameters in the form can be received as String or String [].> 1 ). struts2 provides a built-in type converter that can G type and 8 basic types are automatically converted.> 2 ). struts2 built-in type converter, can be. util. the Date and String types are automatically converted, but the String format of the Date type must be in the yyyy-MM-dd format. Meanwhile, struts2 automatically checks the validity of the Date, for example, 01-33 cannot pass.> 3 ). the built-in converter cannot convert the String type to any javaBean type. You need to use a custom converter here. the following example shows the Address. java copy code package type;/*** @ ClassName: Address * @ Description: defines an address model * @ author: amosli * @ email: amosli@infomorrow.com * @ date Jan 26,201 4 1:53:33 AM */public class Address {private Str Ing province; // province private String city; // city private String area; // Region public String getProvince () {return province;} public void setProvince (String province) {this. province = province;} public String getCity () {return city;} public void setCity (String city) {this. city = city;} public String getArea () {return area;} public void setArea (String area) {this. area = area ;}} copy the code TypeAction. Java copy code package type; import java. util. date; import com. opensymphony. xwork2.ActionSupport;/*** @ ClassName: TypeAction * @ Description: This class mainly describes type conversion * @ author: amosli * @ email: amosli@infomorrow.com * @ date Jan 26,201 4 1:00:35 AM */public class TypeAction extends ActionSupport {private static final long serialVersionUID =-2558635842755601_l; private String username; // name private Integer age; // age pr Ivate Float salary; // salary private Date birthday; // birthday private Address address Address; // address public String getUsername () {return username;} public void setUsername (String username) {this. username = username;} public Integer getAge () {return age;} public Address getAddress () {return address;} public void setAddress (Address address Address) {this. address = address;} public void setAge (Integer age) {this. age = Age;} public Float getSalary () {return salary;} public Date getBirthday () {return birthday;} public void setBirthday (Date birthday) {this. birthday = birthday;} public void setSalary (Float salary) {this. salary = salary;} public String execute () throws Exception {return SUCCESS;} copy the code type_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 = "type" extends = "struts-default "> <action name =" TypeAction "class =" type. typeAction "method =" execute "> <result name =" success "type =" dispatcher ">/WEB-INF/type_success.jsp </result> <result name =" input "type =" dispatcher "> type. jsp </result> </ac Tion> </package> </struts> copy the sturts. xml code. <? 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> <! -- <Include file = "config/upload. xml"> </include> --> <! -- Load other configuration files --> <! -- <Include file = "config/upload-interceptor.xml"> </include> --> <! -- Load the property file-internationalization --> <! -- <Constant name = "struts. custom. i18n. resources" value = "message"> </constant> --> <! -- Result set --> <! -- <Include file = "config/result_struts.xml"> </include> --> <! -- Type conversion --> <include file = "config/type_struts.xml"> </include> </struts> copy the code type. jsp copy Code <% @ 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">

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.