获取SSM项目中所有的URL

1、在springmvc配置加上两个bean:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>  
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>  

2、在controller注入RequestMappingHandlerAdapter:

@Autowired
RequestMappingHandlerMapping requestMappingHandlerMapping;

3、controller添加mapping:

@RequestMapping("/index.action")
    @ResponseBody
    public Object index(HttpServletRequest request) {

        List<HashMap<String, String>> urlList = new ArrayList<HashMap<String, String>>();

        Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            RequestMappingInfo info = m.getKey();
            HandlerMethod method = m.getValue();
            PatternsRequestCondition p = info.getPatternsCondition();
            for (String url : p.getPatterns()) {
                hashMap.put("url", url);
            }
            hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
            hashMap.put("method", method.getMethod().getName()); // 方法名
            RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
            String type = methodsCondition.toString();
            if (type != null && type.startsWith("[") && type.endsWith("]")) {
                type = type.substring(1, type.length() - 1);
                hashMap.put("type", type); // 方法名
            }
            urlList.add(hashMap);
        }
        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put("str", urlList);
        return result;
    }

注:如果实例化RequestMappingHandlerMapping出错,大概是你项目不仅仅只有一个实例,此时请为你写的bean设置id

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要将上传和下载功能集成到SSM项目,可以按照以下步骤操作: 1. 在项目添加文件上传和下载的相关依赖包,例如commons-fileupload和commons-io。 2. 编写上传和下载的Controller层代码,可以参考以下示例代码: 上传文件: ``` @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) @ResponseBody public String uploadFile(HttpServletRequest request, HttpServletResponse response) { String result = ""; try { // 获取上传的文件 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = multipartRequest.getFile("file"); // 获取文件名 String fileName = file.getOriginalFilename(); // 拼接文件存储的路径 String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"; File fileDir = new File(filePath); if (!fileDir.exists()) { fileDir.mkdirs(); } // 将上传的文件保存到指定路径 file.transferTo(new File(filePath + fileName)); result = "success"; } catch (Exception e) { result = "error"; e.printStackTrace(); } return result; } ``` 下载文件: ``` @RequestMapping(value = "/downloadFile", method = RequestMethod.GET) public void downloadFile(HttpServletRequest request, HttpServletResponse response) { String fileName = "test.txt"; String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"; try { // 设置响应头 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); // 获取要下载的文件流 InputStream inputStream = new FileInputStream(filePath + fileName); OutputStream outputStream = response.getOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, len); } inputStream.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } ``` 3. 在页面添加上传和下载的相关代码,例如: 上传文件: ``` <form id="uploadForm" action="uploadFile" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="button" value="上传" onclick="uploadFile()"/> </form> <script> function uploadFile() { $.ajax({ type: "POST", url: "uploadFile", data: new FormData($("#uploadForm")[0]), async: false, cache: false, contentType: false, processData: false, success: function (data) { alert(data); }, error: function () { alert("上传失败!"); } }); } </script> ``` 下载文件: ``` <a href="downloadFile">下载</a> ``` 4. 运行项目,测试上传和下载功能是否正常。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值