The Handler + Context architecture is the foundation of Solon Web. It can be obtained in Context (org.noear.solon.core.handle.Context):
- Request related objects and interfaces
- Session state related objects and interfaces
- Response related objects and interfaces
Or understand that all requests and responses are related to it. Regarding the architecture, you can take a look at "Notes on Ideas and Architecture"
1. Three ways to obtain Context
a) Obtained through Controller
@Controller
public class HelloController{
@Mapping("/hello")
public String hello(Context ctx){
//可以注入 ctx:Context
return "Hello " + ctx.param("name", "world");
}
}
b) Obtained through Handler or Filter or RouterInterceptor interface
Solon.start(DemoApp.class, args, app->{
app.get("/hello", ctx-> ctx.output("Hello " + ctx.param("name", "world")));
});
//或者,用以组件方式编写
@Mapping("/hello")
@Component
public class HelloHandler implements Handler{
public void handle(Context ctx) throws Throwable{
ctx.output("Hello " + ctx.param("name", "world"));
}
}
c) Direct acquisition (implemented based on ThreadLocal)
Context ctx = Context.current();
2. Similar effects on context-path (based on pathNew)
Just add configuration : (supported after v1.11.2)
server.contextPath: "/test-service/"
It can also be handled manually :
public class DemoApp{
public static void main(String[] args){
Solon.start(DemoApp.class, args, app->{
//使用专用过滤器
app.filter(-99, new ContextPathFilter("/xxx/"));
//使用专用过滤器(原生路径将不能访问,不建议使用)
//app.filter(-99, new ContextPathFilter("/xxx/", true));
});
}
}
3. Request related interfaces
Request related interface | illustrate |
---|---|
-request()->Object | raw request object |
-ip()->String | Get the source request IP (it may also be the proxy IP) |
-realIp()->String | Get the real IP of the client |
-isMultipart()-bool | Is it segmented content |
-isMultipartFormData()->bool | Whether it is segmented form data |
-method()->String | Get request method |
-protocol()->String | Get Request Protocol |
-protocolAsUpper()->String | Get request protocol and capitalize |
-url()->String | Get the requested URL string |
uri()->URI | Get the requested URI |
-path()->String | Get the requested URI path |
-pathNew(String) | set new path |
-pathNew()->String | Get the new path, if it does not exist, return the original path |
-pathMap(String)->NvMap | Get the requested URI path variable, based on the path expression |
-pathAsUpper()->String | Get the requested URI path and capitalize it |
-pathAsLower()->String | Get the requested URI path and lowercase it |
-userAgent()>String | Get the requested UA |
-contentLength()->long | get content length |
-contentType()->String | get content type |
-queryString()->String | get query string |
-accept()->String | Get Accept header information |
-body()->String | Get body content |
-body(String)->String | Get the body content and decode it according to the specified string |
-bodyNew()->String | get new body |
-bodyNew(String) | set new body |
-bodyAsBytes()->byte[] | Get the body content as byte[] |
-bodyAsStream()->InputStream | Get body content as Stream |
-paramValues(String)->String[] | get parameter array |
-param(String)->String | Get parameters |
-param(String, String)->String | Get parameters and give default values |
-paramAsInt(String)->int | Get parameters and convert to int |
-paramAsInt(String, int)->int | Get the parameters and convert them to int, and give them a default value |
-paramAsLong(String)->long | Get the parameter and convert it to long |
-paramAsLong(String, long)->long | Get the parameter and convert it to long, and give a default value |
-paramAsDouble(String)->double | Get the parameters and convert them to double |
-paramAsDouble(String, double)->double | 获取参数并转为double,并给定默认值 |
-paramAsDecimal(String)->BigDecimal | 获取参数并转为BigDecimal |
-paramAsDecimal(String, BigDecimal)->BigDecimal | 获取参数并转为BigDecimal,并给定默认值 |
-paramAsBean(Class<T> )->T |
获取参数并转为Bean |
-paramMap()->NvMap | 获取所有参数并转为map |
-paramsMap()->Map<String, List<String>> |
获取所有参数并转为Map |
-paramSet(String, String) | 设置参数 |
-paramsAdd(String, String) | 添加参数 |
-filesMap()->Map<String,List<UploadedFile>> |
获取所有上传的文件 |
-files(String)->List<UploadedFile> |
获取上传文件,可能有多个 |
-file(String)->UploadedFile | 获取上传文件,第一个 |
-cookie(String)->String | 获取 cookie |
-cookie(String, String)->String | 获取 cookie, 并给定默认值 |
-cookieMap()->NvMap | 获取 cookieMap |
-header(String)->String | 获取 header |
-header(String, String)->String | 获取 header,并给定默认值 |
-headerValues(String)->String | 获取 header 数组 |
-headerMap()->NvMap | 获取 headerMap |
-headersMap()->Map<String, List<String>> |
获取 headersMap |
4、响应相关的接口
响应相关接口 | 说明 |
---|---|
-response()->Object | 原始响应对象 |
-charset(String) | 设置字符集 |
-contentType(String) | 设置内容类型 |
-contentTypeNew() | 获取设置的新内容类型 |
-render(Object) | 渲染数据(比如将对象渲染为 Json 并输出) |
-render(String, Map) | 渲染视图 |
-renderAndReturn(Object)->String | 渲染数据并返回 |
-output(byte[]) | 输出 字节数组 |
-output(InputStream) | 输出 流对象 |
-output(String) | 输出 字符串 |
-output(Throwable) | 输出 异常对象 |
-outputAsJson(String) | 输出为json文本 |
-outputAsHtml(String) | 输出为html文本 |
-outputAsFile(DownloadedFile) | 输出为文件 |
-outputAsFile(File) | 输出为文件 |
-outputStream()->OutputStream | 获取输出流 |
-flush() | 冲刷 |
-headerSet(String, String) | 设置 header |
-headerAdd(String, String) | 添加 header |
-cookieSet(String, String) | 设置 cookie |
-cookieSet(String, String, int) | 设置 cookie |
-cookieSet(String, String, String, int) | 设置 cookie |
-cookieSet(String, String, String, String, int) | 设置 cookie |
-cookieRemove(String) | 移徐 cookie |
-redirect(String) | 302跳转地址 |
-redirect(String, int) | 跳转地址 |
-forward(String) | 服务端转换地址 |
-status() | 获取输出状态 |
-status(int) | 设置输出状态 |
5、会话相关的接口
会话相关接口 | 说明 |
---|---|
-sessionState()->SessionState | 获取 sessionState |
-sessionId()->String | 获取 sessionId |
-session(String)->Object | 获取 session 状态 |
-session(String, T)->T | 获取 session 状态(类型转换,存在风险) |
-sessionAsInt(String)->int | 获取 session 状态以 int 型输出 |
-sessionAsInt(String, int)->int | 获取 session 状态以 int 型输出, 并给定默认值 |
-sessionAsLong(String)->long | 获取 session 状态以 long 型输出 |
-sessionAsLong(String, long)->long | 获取 session 状态以 long 型输出, 并给定默认值 |
-sessionAsDouble(String)->double | 获取 session 状态以 double 型输出 |
-sessionAsDouble(String, double)->double | 获取 session 状态以 double 型输出, 并给定默认值 |
-sessionSet(String, Object) | 设置 session 状态 |
-sessionRemove(String) | 移除 session 状态 |
-sessionClear() | 清空 session 状态 |
6、其它查询
其它相关接口 | 说明 |
---|---|
+current()->Context | 获取当前线程的上下文 |
-getLocale()->Locale | 获取地区 |
-setLocale(Locale) | 设置地区 |
-setHandled(bool) | 设置处理状态 |
-getHandled() | 获取处理状态 |
-setRendered(bool) | 设置渲染状态 |
-getRendered() | 获取渲染状态 |
-attrMap()->Map | 获取自定义特性并转为Map |
-attr(String)->Object | 获取上下文特性 |
-attr(String, T)->T | 获取上下文特性,并设定默认值 |
-attrSet(String, Object) | 设置上下文特性 |
-attrSet(Map) | 设置上下文特性 |
-attrClear() | 清除上下文特性 |
-remoting()->bool | 是否为远程调用 |
-remotingSet(bool) | 设置是否为远程调用 |
-result:Object | 用于在处理链中透传处理结果 |
-errors:Throwable | 用于在处理链中透传处理错误 |
-controller()->Object | 获取当前控制器 |
-action()->Action | 获取当前动作 |