对于从SpringController获取Web应用程序根目录感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍spring如何获取controller,并为您提供关于3.springbootCo
对于从Spring Controller获取Web应用程序根目录感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍spring如何获取controller,并为您提供关于3.spring boot Controller获取请求参数的值、asp.net – 在启动期间获取Web应用程序的URI、java – 在运行ng serve时将angular嵌入spring应用程序并访问Spring Controllers、javascript获取web应用根目录的方法的有用信息。
本文目录一览:- 从Spring Controller获取Web应用程序根目录(spring如何获取controller)
- 3.spring boot Controller获取请求参数的值
- asp.net – 在启动期间获取Web应用程序的URI
- java – 在运行ng serve时将angular嵌入spring应用程序并访问Spring Controllers
- javascript获取web应用根目录的方法
从Spring Controller获取Web应用程序根目录(spring如何获取controller)
我正在尝试将上载的多部分文件写入文件系统。我有一个名为audio的目录,该目录位于Web应用程序的根目录中(不在WEB-
INF内,但在它旁边,可以像CSS和javascript一样公开访问)。
我想将上传的文件写入该目录,但似乎无法获取所需的路径。我以为使用ServletPath()然后使用realPath()可能可行,但是我没有通过Spring控制器对ServletContext的引用。谢谢你的帮助
@RequestMapping(value="/uploadSample")public ModelAndView upload(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile f) { if (f == null) { return new ModelAndView("upload", "msg", "The file is null."); } try { // I need to set AUDIO_PATH to <webAppRoot>/audio FileOutputStream file = new FileOutputStream(AUDIO_PATH + "/" + f.getOriginalFilename()); file.write(f.getBytes()); file.close(); } catch (FileNotFoundException ex) { Logger.getLogger(SampleUploadController.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(SampleUploadController.class.getName()).log(Level.SEVERE, null, ex); } return new ModelAndView("upload", "msg", "File ( " + f.getOriginalFilename() + ") successfully uploaded.");}
}
答案1
小编典典我认为先获取ServletContext()然后使用realPath()可能有效,但是我没有对ServletContext的引用
是的你是。参见HttpServletRequest.getSession()。getServletContext()
3.spring boot Controller获取请求参数的值
1.获取连接中的参数,使用倒的关键词@PathVariable
@RestController public class HelloController { @RequestMapping(value = "/hello/{id}",method = RequestMethod.GET) public String index(@PathVariable("id") Integer id){ return "id="+id; } }
启动项目访问,成功获取到id
也可以把id放在/hello前面
public class HelloController {
@RequestMapping(value = "/{id}/hello",method = RequestMethod.GET)
public String index(@PathVariable("id") Integer id){
return "id="+id;
}
}
2.传统的问号(?id=110)传值,设置id可以不传默认值为0,使用倒的关键词@RequestParam
public class HelloController {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String index(@RequestParam(value = "id" ,required = false, defaultValue = "0") Integer id){
return "id="+id;
}
}
不传参数默认为0
传参数为获取到的参数
asp.net – 在启动期间获取Web应用程序的URI
那我怎么能正确地确定应用程序的基URI?我认为最适合的地方是Global.asax.cs文件,在网络应用程序启动期间,但我找不到任何看起来有用的东西.
解决方法
> ApplicationVirtualPath – > “/我的应用程序”
> SiteName => “默认网站”
但是,在实际请求进入之前,您将无法获取域名.这是因为IIS网站可以接受许多不同域的请求,其中一些域只能通过DNS获知,并且从未在任何地方配置.
例如,您的网站可以响应mydomain.com和www.mydomain.com.您想要在链接中添加哪个是正确的?
您可以将IIS网站配置为仅接受请求特定主机但无法从ASP.NET应用程序检索的连接.
java – 在运行ng serve时将angular嵌入spring应用程序并访问Spring Controllers
我打算设置一个Spring-Angular应用程序.我立即开始使用Hello World示例来测试如何设置环境.我最终做了什么:
在此应用程序中创建Spring-Project并创建Angular-application.现在我可以通过HttpClient角度模块访问Spring-REST-Controllers. (代码示例见下文).
优点:我可以使用mvn包将Angular和Spring部分打包到一个jar中,然后将它部署到我的tomcat上.可悲的是,当我运行服务时,只执行了前端,我无法访问后端的数据.有没有办法设置我的环境,以便我可以获得单项目解决方案的优势,仍然使用ng服务来测试它?
我尝试了什么:
打包并通过终端(java -jar%jar-file%)执行它,并使用localhost:8080 / hello作为我的HttpClient的路径而不是简单/ hello.遗憾的是,这没有成功.
到目前为止我得到的代码:
app.component.ts
import { Component, OnInit } from ''@angular/core'';
import { HttpClient } from ''@angular/common/http'';
@Component({
selector: ''app-root'',
templateUrl: ''./app.component.html'',
styleUrls: [''./app.component.css'']
})
export class AppComponent implements OnInit {
title = ''HelloWorld'';
message: string;
constructor(private http : HttpClient) {}
ngOnInit() : void {
//this is where I tried to use localhost:8080/hello instead
this.http.get(''/hello'').subscribe( data => {
console.log(''DATA'', data);
this.message = data[''message''];
});
}
}
REST的控制器:
package com.example.helloWorld.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("/hello")
public String sayHello() {
return "{\"message\": \"Hello, World!\"}";
}
}
的pom.xml
http://maven.apache.org/xsd/maven-4.0.0.xsd\”\u0026gt;
4.0.0
<groupId>com.example</groupId>
<artifactId>helloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helloWorld</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.0.RELEASE</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>ng</executable>
<workingDirectory>src/main/ui</workingDirectory>
<arguments>
<argument>build</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
解决方法:
这样做
this.http.get(''/hello'').subscribe( data => {
console.log(''DATA'', data);
this.message = data[''message''];
});
您需要执行一些代理配置.
在项目内的package.json所在的同一目录中创建一个proxy-config.json文件.
{
"/": {
"target": "http://localhost:8080",
"secure": false
}
}
在package.json里面脚本用“start”更新“start”命令:“ng serve –proxy-config proxy-config.json”,
之后尝试使用npm start命令来运行您的项目.
总结
以上是小编为你收集整理的java – 在运行ng serve时将angular嵌入spring应用程序并访问Spring Controllers全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
原文地址:https://codeday.me/bug/20191002/1841657.html
javascript获取web应用根目录的方法
<script>
function getRootPath(){
var strFullPath=window.document.location.href;
var strPath=window.document.location.pathname;
var pos=strFullPath.indexOf(strPath);
var prePath=strFullPath.substring(0,pos);
var postPath=strPath.substring(0,strPath.substr(1).indexOf(‘/')+1);
return(prePath+postPath);
}
var webpath=getRootPath(); //webpath就是目录路径变量
</script>
今天关于从Spring Controller获取Web应用程序根目录和spring如何获取controller的讲解已经结束,谢谢您的阅读,如果想了解更多关于3.spring boot Controller获取请求参数的值、asp.net – 在启动期间获取Web应用程序的URI、java – 在运行ng serve时将angular嵌入spring应用程序并访问Spring Controllers、javascript获取web应用根目录的方法的相关知识,请在本站搜索。
本文标签: