struts基础03_异常处理_json数据

1.异常处理

(1)创建自定义异常类

package net.dfrz.exception;

public class MyException extends Exception {

private String message;

 

public MyException(String message) {

this.message = message;

}

 

public String getMessage() {

return message;

}

 

public void setMessage(String message) {

this.message = message;

}

}

(2)在struts.xml中配置全局异常

<global-results>

<result name="myexc">/exception.jsp</result>

</global-results>

<global-exception-mappings>

<exception-mapping result="myexc" exception="

net.dfrz.exception.MyException"></exception-mapping>

</global-exception-mappings>

(3)创建测试类

package net.dfrz.action;

import net.dfrz.exception.MyException;

public class Hello2Action

{

public String hello2() throws MyException

{

System.out.println("message 2:"+message);

try

{

int i = 10/0;

}

catch(Exception e)

{

throw new MyException("除0异常");

}

return "success";

}

private String message;

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

}

(4)捕获异常信息

<body>

我是exception

<hr/>

${exception.message}

</body>

 

2.返回json数据类型

(1)jar包更新

(2)修改struts.xml配置(继承json-default)

<package name="javastruts" extends="json-default">

 

(3)测试代码

@Actions({

@Action(value="/findPersonById",results={

@Result(name="success",location="/person.jsp")

}),

@Action(value="/findPersonById2json",results={

//params={"root","person","excludeNullProperties","true"})

//json格式 只有person对象的数据 不包含空字符

@Result(type="json",

params= {"ignoreHirearchy","false","includeProperties",

"person.*,pageNO,pageSize","excludeNullProperties","true"})

// ignoreHirearchy-false表示可过滤继承 includeProperties包含

})

})

public String findPersonById(){

person = new Person();

person.setAge(11);

person.setName("sda");

person.setBirthday(new Date());

return "success";

}

@Actions({

@Action(value="/findPersonById4json",results={

@Result(type="json",params={"includeProperties",

"lists\\[\\d+\\]\\.\\w+,pageNO,pageSize"})

//返回列表 \d数字 \w字母

})

})

public String findPersons(){

lists = new ArrayList<Person>();

person = new Person();

person.setAge(11);

person.setName("sda");

person.setBirthday(new Date());

lists.add(person);

 

person = new Person();

person.setAge(22);

person.setName("222");

person.setBirthday(new Date());

lists.add(person);

 

person = new Person();

person.setAge(33);

person.setName("333");

person.setBirthday(new Date());

lists.add(person);

return "success";

}

(4)相关类

public class Person implements Serializable{

private static final long serialVersionUID = 1501197896931045497L;

private String name;

private int age;

private Date birthday;

 

@JSON(format="yyyy:MM:dd")

public Date getBirthday() {

return birthday;

}

 

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

 

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

public int getAge() {

return age;

}

 

public void setAge(int age) {

this.age = age;

}

}

//

public class CommonAction extends ActionSupport{

 

protected int pageNO;

protected int pageSize;

public int getPageNO() {

return pageNO;

}

public void setPageNO(int pageNO) {

this.pageNO = pageNO;

}

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

this.pageSize = pageSize;

}

}

猜你喜欢

转载自blog.csdn.net/weixin_42756687/article/details/81272711
今日推荐