Solon Web 开发:二、开发知识准备

1、约定

//资源路径约定(不用配置;也不能配置)
resources/app.yml( 或 app.properties ) #为应用配置文件

resources/WEB-INF/static/ 或者 resources/static/     #为静态文件根目录(目录二选一,v2.2.10 后支持)
resources/WEB-INF/templates/   或者 resources/templates/  #为视图模板文件根目录,支持多种视图共存(目录二选一,v2.2.10 后支持)

//调试模式约定:
启动参数添加:--debug=1

2、应用启动过程

请参考: 《应用生命周期》 的上两段。

3、服务端口配置

在应用主配置文件里指定:

server.port: 8081

可以在运行时指定系统属性(优先级高):

java -Dserver.port=9091 -jar DemoApp.jar

还可以,在运行时通过启动参数指定(优先级更高):

java -jar DemoApp.jar -server.port=9091

4、静态资源放哪里

Solon 的默认静态资源的路径为:

resources/static/

这个默认没得改,但是可以添加更多(具体可参考:《生态 / solon.web.static》):

#添加静态目录映射。(按需选择)#v1.11.0 后支持
solon.staticfiles.mappings:
  - path: "/img/" #路径,可以是目录或单文件
    repository: "/data/sss/app/" #1.添加本地绝对目录(仓库只能是目录)
  - path: "/"
    repository: "classpath:user" #2.添加资源路径(仓库只能是目录)
  - path: "/"
    repository: ":extend" #3.添加扩展目录

在默放的处理规则下,所有请求,都会先执行静态文件代理。静态文件代理会检测是否存在静态文件,有则输出,没有则跳过处理。输出的静态文件会做304控制。

框架不支持 “/” 自动跳转到 “/index.html” 的方式(现在少见了,也省点性能)。如有需要,可添过滤器手动处理:

@Component(index = 0) //index 为顺序位(不加,则默认为0)
public class AppFilter implements Filter {
    
    
    @Override
    public void doFilter(Context ctx, FilterChain chain) throws Throwable {
    
    
        if("/".equals(ctx.pathNew())){
    
     //ContextPathFilter 就是类似原理实现的
            ctx.pathNew("/index.html");
        }

        chain.doFilter(ctx);
    }
} 

5、路径映射表达式说明

符号 说明 示例
** 任意字符、不限段数 **/user/**
* 任意字符 /user/*
? 可有可无 /user/?
/ 路径片段开始符和间隔符 //user
{name} 路径变量申明 /user/{name}

具体可参考:《常用注解 / @Mapping 用法说明》

6、路径重定向与转发

路径重定向

public class DemoController{
    
    
    @Mapping("/")
    public void home(Context ctx) {
    
    
        //通过302方式,通知客户端跳转到 “/index.html” (浏览器会发生2次请求,地址会变成/login)
        ctx.redirect("/index.html");
    }
}    

路径转发(不支持转发到静态文件,静态处理与路由处理是两个体系)

public class DemoController{
    
    
    @Mapping("/")
    public void home(Context ctx) {
    
    
        //在服务端重新路由到 “/index.html” (浏览器发生1次请求,地址不会变)
        ctx.forward("/index.html");
    }
}    

7、请求参数注入或手动获取

支持 queryString, form-data, x-www-form-urlencoded, path, json body 等不同形式的参数直接注入:

//GET http://localhost:8080/test1?str=a&num=1
public class DemoController{
    
    
    @Mapping("/test1")
    public void test1(String str, int num) {
    
     //可自动注入 get, post, json body 形式的参数
        
    }
    
    @Mapping("/test2")
    public void test2(Context ctx) {
    
      
        //手动获取 get, post 参数
        String str = ctx.param("str");
        int num = ctx.paramAsInt("num", 0);
        
        //手动获取 body 请求的数据
        String body = ctx.body();
    }
}

其中 queryString, form-data, x-www-form-urlencoded, path 参数,支持 ctx.param() 接口统一获取。

8、统一请求异常处理和性能计时

//过滤所有因请求产生的异常(顺带加点别的)
@Slf4j
@Component
public class AppFilter implements Filter {
    
    
    @Override
    public void doFilter(Context ctx, FilterChain chain) throws Throwable {
    
    
        //1.开始计时(用于计算响应时长)
        long start = System.currentTimeMillis();
        try {
    
    
            chain.doFilter(ctx);

            //2.未处理设为404状态
            if(! ctx.getHandled()){
    
    
                ctx.status(404);
            }
            
            //3.404状态的定制(也可对别的状态处理)
            if (ctx.status() == 404) {
    
     
                ctx.setHandled(true);
                ctx.output("没有:(");
            }
        } catch (Throwable e) {
    
    
            //4.异常捕促与控制
            log.error(e);
        }

        //5.获得接口响应时长
        long times = System.currentTimeMillis() - start;
        System.out.println("用时:"+ times);
    }
}  

9、读取自定义的配置文件

//直接注入
@Configuration
public class Config{
    
    
    @Inject("${classpath:user.yml}")
    private UserModel user;
}

public class DemoApp{
    
    
    public static void main(String[] args){
    
    
        Solon.start(DemoApp.class, args, app->{
    
    
            //加载到应用配置
            app.cfg().loadAdd("user.yml");
        });
    }
}

10、也有jsp的支持(不建议用)

solon 的jsp支持,是基于视图模板的定位去处理的。根据启动器组件的不同,配置略有不同:

<!-- 添加 solon web 开发包 -->
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-web</artifactId>
</dependency>

<!-- 添加 jetty 或 undertow 启动器 -->
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon.boot.jetty</artifactId>
</dependency>

<!-- 添加 jetty 或 undertow jsp 扩展支持包 -->
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon.boot.jetty.add.jsp</artifactId>
</dependency>

<!-- 添加 jsp 视图引擎 -->
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon.view.jsp</artifactId>
</dependency>

猜你喜欢

转载自blog.csdn.net/cwzb/article/details/131527107
今日推荐