该文章是对上一篇文章的补充主要演示该框架的功能,若需查看其实现原理请看上一篇文章。
首先引入bamboo.jar包,该框架不依赖于任何第三方jar包,然后配置web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<display-name>BaseFilter</display-name>
<filter-name>BaseFilter</filter-name>
<filter-class>com.loveight.filter.BaseFilter</filter-class>
<init-param>
<param-name>package</param-name>
<param-value>com.loveight</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>BaseFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这里我写的Action都要在com.loveight包下,否则不会扫描到该Action类文件,访问时就会抛404错误
这样这个环境就搭建好了!开始测试功能
测试1:
package com.loveight.action;
import com.loveight.annotation.classes.Action;
import com.loveight.annotation.method.Value;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 18-6-6
* Time: 下午12:32
* To change this template use File | Settings | File Templates.
*/
@Action("/test")
public class Test {
private int size;
@Value("/value")
public String to() {
return String.valueOf(size);
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
测试结果如下:
测试2:
package com.loveight.action;
import com.loveight.annotation.classes.Action;
import com.loveight.annotation.field.Type;
import com.loveight.annotation.method.Value;
import java.util.Arrays;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 18-6-6
* Time: 下午12:32
* To change this template use File | Settings | File Templates.
*/
@Action("/test")
public class Test2 {
@Type(int.class)
private int[] size;
@Value("/value2")
public String to() {
return Arrays.toString(size);
}
public int[] getSize() {
return size;
}
public void setSize(int[] size) {
this.size = size;
}
}
源码说明:
@Type(int.class)表示告诉框架参数封装到size数组的类型,此处表示数组的类型是int类型
测试结果如下:
测试3:
@Action("/test")
public class Test2 {
@Type(int.class)
private List size;
@Value("/value2")
public String to() {
return size.toString();
}
public List getSize() {
return size;
}
public void setSize(List size) {
this.size = size;
}
}
测试结果:
进行测试4之前先增加两个bean代码如下:
package com.loveight.domain;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 18-6-6
* Time: 下午12:52
* To change this template use File | Settings | File Templates.
*/
public class Person {
private String idcard;
private String name;
private int age;
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
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;
}
@Override
public String toString() {
return "Person{" +
"idcard='" + idcard + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.loveight.domain;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 18-6-6
* Time: 下午12:51
* To change this template use File | Settings | File Templates.
*/
public class Student {
private Person person;
private String grade;//年级
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Student{" +
"person=" + person +
", grade='" + grade + '\'' +
'}';
}
}
测试4:
@Action("/test")
public class Test3 {
private Student student;
@Value("/value4")
public String to() {
return student.toString();
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
测试结果:
测试5:
@Action("/test")
public class Test3 {
@Session("student")
private Student student;
@Value("/value4")
public String to() {
return student.toString();
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
@Session("student")表示该字段会自动从session中注入,使用完后会自动更新到session中
@ServletContext("student")和上述类似表示会从servletContext中注入,使用完后会自动更新到servletContext中,这里不做演示
结果:
第一次请求带参数:
第二次请求:去掉参数但是参数已经设置到session中了:
测试6:
@Action("/test")
public class Test5 {
private Student student;
@Forward("/value5")
public String to() {
return "/index.jsp";//使用注解Forward表示转发,这里返回的是jsp视图页面
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
jsp页面:
%--
Created by IntelliJ IDEA.
User: Administrator
Date: 18-6-6
Time: 下午12:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
这里已经转发到jsp页面里来了:<br>
${student.grade} <br>
${student.person.name} <br>
${student.person.age} <br>
</body>
</html>
结果:
测试7:
@Action("/test")
public class Test6 {
private Student student;
@Forward("/value5")
public String to() {
return "/index.jsp";
}
@Forward("/value6")
public String forward() {
return "/test/value5";//使用注解Forward表示转发,这里将转发到url /test/value5里面
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
结果:
同上(测试6结果)
测试8:
如果想操作session和servletcontext对象可以让自己的action类继承Context,如果想操作request和response则继承SuperContext类即可在action中使用。
测试9:
@Action("/test")
public class Test6 extends SuperContext {
private Student student;
@Forward("/value5")
public String to() {
return "/index.jsp";
}
@Redirect("/value7")
public String redirect() {
return "/test/value5";//使用注解Redirect表示重定向,这里将重定向到url /test/value5里面
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
结果:输入访问地址:
回车后:因重定向url会变化且两次请求数据不共享,结果如下:
基本功能结束……