解析url

本文展示了如何在Spring MVC中处理HTTP请求参数,包括普通方式和RESTful风格。`CarController.java`中演示了不同参数接收方式,如通过对象封装、基本类型和引用类型。`CarController2.java`则介绍了使用`@PathVariable`注解实现RESTful API,以更简洁的方式获取路径变量。

CarController.java

package cn.tedu.controller;

import cn.tedu.pojo.Car;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController//接受请求,并把json数据返回
@RequestMapping("car")//规定了url地址的写法
public class CarController {
    //springmvc框架解析请求中的参数
    @RequestMapping("get5")
    //http://localhost:8091/car/get5?id=10&name=BMW&price=9.9
    public void get5(Car c){//springmvc框架会把请求的参数,封装给car对象
        System.out.println(c.getId()+c.getName()+c.getPrice());
    }
    //http://localhost:8091/car/get4?id=10&name=BMW
    @RequestMapping("get4")
    public void get4(Integer id,String name){
        //id是用来接受url里id的值,name用来接受url里name的值
        System.out.println(id+name);
    }
    //http://localhost:8091/car/get3?id=10
    @RequestMapping("get3")
    //public void get3(int id){//参数是基本类型,访问这个方法必须带参数,否则有异常
    public void get3(Integer id){//参数是引用类型,访问这个方法没带参数就是null
        System.out.println(id);
        }
        //自己解析请求中的参数
        public void get2(){
        String url="http://localhost:8091/car/get2?id=10&name=BMW&price=9.9";
            String[] s = url.split("\\?")[1].split("&");
            for(String ss:s){
                String key=ss.split("=")[0];
                String value=ss.split("=")[1];
            }
        }

        public Car get(){
            Car c = new Car(10,"BMW",19.9);
            return c;
        }
}

CarController2.java

package cn.tedu.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*对比请求参数的不同获取方式*/
@RestController
@RequestMapping("car2")
public class CarController2 {
    //1普通的get方式获取请求参数
    //http://localhost:8091/car2/get?id=10&name=BMW&age=10&sex=1
    @RequestMapping("get")
    public String get(Integer id,String name,Integer age,Integer sex){
       //return id+name+age+sex;
       //return id+name+age+sex;//组织成json串给浏览器展示
        return "{"+"id"+":"+id+"}";//{id:10}
    }
    //2.restful方式获取请求参数:通过{}绑定地址中参数的位置+通过注解获取{???}的值
    //restful简化get提交的数据的写法
    //http://localhost:8091/car2/get2/10/BMW/10/1
    @RequestMapping("get2/{id}/{name}/{x}/{y}")
    public void get2(@PathVariable Integer id,
                     @PathVariable String name,
                     @PathVariable String x,
                     @PathVariable Integer y ){
        System.out.println(id);
        System.out.println(name);
        System.out.println(x);
        System.out.println(y);
    }

}

 

内容概要:本文介绍了一个基于Matlab的综合能源系统优化调度仿真资源,重点实现了含光热电站、有机朗肯循环(ORC)和电含光热电站、有机有机朗肯循环、P2G的综合能源优化调度(Matlab代码实现)转气(P2G)技术的冷、热、电多能互补系统的优化调度模型。该模型充分考虑多种能源形式的协同转换与利用,通过Matlab代码构建系统架构、设定约束条件并求解优化目标,旨在提升综合能源系统的运行效率与经济性,同时兼顾灵活性供需不确定性下的储能优化配置问题。文中还提到了相关仿真技术支持,如YALMIP工具包的应用,适用于复杂能源系统的建模与求解。; 适合人群:具备一定Matlab编程基础和能源系统背景知识的科研人员、研究生及工程技术人员,尤其适合从事综合能源系统、可再生能源利用、电力系统优化等方向的研究者。; 使用场景及目标:①研究含光热、ORC和P2G的多能系统协调调度机制;②开展考虑不确定性的储能优化配置与经济调度仿真;③学习Matlab在能源系统优化中的建模与求解方法,复现高水平论文(如EI期刊)中的算法案例。; 阅读建议:建议读者结合文档提供的网盘资源,下载完整代码和案例文件,按照目录顺序逐步学习,重点关注模型构建逻辑、约束设置与求解器调用方式,并通过修改参数进行仿真实验,加深对综合能源系统优化调度的理解。
### 关于URL解析的方法 在编程领域,URL解析通常涉及提取协议、主机名、路径、查询参数以及片段标识符等内容。以下是几种常见的URL解析方法: #### 使用正则表达式解析URL 通过编写正则表达式可以手动解析URL中的各个部分。这种方法灵活性较高,但也可能较为复杂[^1]。 ```python import re url = "https://www.example.com/path/to/resource?query=param#fragment" pattern = r'^(https?):\/\/([^\/]+)([/\w\.]*)\??([\w=&]*)#?(\S*)' match = re.match(pattern, url) if match: protocol, host, path, query, fragment = match.groups() print(f"Protocol: {protocol}, Host: {host}, Path: {path}, Query: {query}, Fragment: {fragment}") ``` #### 利用标准库进行URL解析 许多现代编程语言提供了内置的标准库来简化URL解析过程。例如,在Python中可以使用`urllib.parse`模块[^2]。 ```python from urllib.parse import urlparse, parse_qs url = "https://www.example.com/path/to/resource?query=param&another=one#fragment" parsed_url = urlparse(url) print(f"Scheme: {parsed_url.scheme}") # 输出 Scheme: https print(f"Netloc: {parsed_url.netloc}") # 输出 Netloc: www.example.com print(f"Path: {parsed_url.path}") # 输出 Path: /path/to/resource print(f"Query: {parse_qs(parsed_url.query)}") # 输出 Query: {'query': ['param'], 'another': ['one']} print(f"Fragment: {parsed_url.fragment}") # 输出 Fragment: fragment ``` #### JavaScript 中的 URL 处理 对于前端开发者来说,JavaScript 提供了 `URL` 和 `URLSearchParams` 对象用于方便地操作 URL 数据结构。 ```javascript const url = new URL('https://www.example.com/path/to/resource?query=param&another=one#fragment'); console.log(`Hostname: ${url.hostname}`); // 输出 Hostname: www.example.com console.log(`Pathname: ${url.pathname}`); // 输出 Pathname: /path/to/resource console.log(`Search Params:`); for (let [key, value] of url.searchParams.entries()) { console.log(`${key}: ${value}`); } // 输出 Search Params: // query: param // another: one console.log(`Hash: ${url.hash}`); // 输出 Hash: #fragment ``` ### 总结 无论是后端还是前端开发,针对不同场景可以选择合适的工具和技术栈来进行 URL 的高效解析。上述提到的技术手段均能有效完成这一目标,并且可以根据实际需求进一步扩展功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值