我是Java和Spring的新手.如何将我的应用程序根映射http://localhost:8080/到静态index.html?如果我导航到http://localhost:8080/index.html它的工作正常.
我的app结构是:

我config\WebConfig.java看起来像这样:
@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
}
Run Code Online (Sandbox Code Playgroud)
我试图添加registry.addResourceHandler("/").addResourceLocations("/index.html");但它失败了.
我正在尝试使用此处的信息在 spring-boot服务器中添加静态Web内容
我尝试在上一个链接所说的几个文件夹中添加我需要的.js和.css文件,但它不起作用:
Run Code Online (Sandbox Code Playgroud)/META-INF/resources/ /resources/
注意:我没有创建/static/和/public/文件夹,因为我不知道项目中的绝对路径.
我也添加了addResourceHandlers方法:
Run Code Online (Sandbox Code Playgroud)@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { if (!registry.hasMappingForPattern("/webjars/**")) { registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); } if (!registry.hasMappingForPattern("/**")) { registry.addResourceHandler("/**").addResourceLocations( RESOURCE_LOCATIONS); } }
HTML文件中的引用如下所示:
<script src="bootstrap-switch.js"></script>
知道如何解决这个问题吗?
更新:

更新2:尝试@jfcorugedo
不起作用.看一下,那是你在说什么?

更新3:尝试@jfcorugedo,第二个建议:

我目前需要使用Spring Boot来提供我的React应用程序.它适用于根URL - localhost:8080/但当然没有子路由被Spring Controller识别.
我不确定如何在没有硬编码的情况下将React Routing和Spring请求映射排成一行,以便映射到每个可能的路由index.html- 然后一些路由具有可变的子路由.
这是我的HomeController服务index.html
@Controller
public class HomeController {
@RequestMapping(value = "/")
public String index() {
return "index.html";
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的React App中的路由渲染
const Root = ({ store }) => (
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRedirect to="users"/>
<Route path="/org" component={OrgSearch}>
{<Route path="/org/:orgId" component={Org}/>}
</Route>
<Route path="/users" component={UserSearch}>
{<Route path="/users/:userId" component={User} />}
</Route>
</Route>
</Router>
</Provider>
)
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏!
编辑:我尝试添加一些通配符功能,它呈现出一些奇怪的行为.这是更新的代码HomeController.
@Controller
public class HomeController {
@RequestMapping(value = …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个应该使用单页React前端的网页.对于后端,我正在使用spring boot框架.
所有api调用都应使用前缀为url的URL,/api并应由REST控制器处理.
所有其他网址应该只是提供index.html文件.如何用弹簧实现这一目标?
所以我试图在一个普通的 REST API 旁边用 spring 托管一个单页应用程序。
这意味着所有发送到普通/api/端点的请求都应该由各自的控制器处理,所有其他请求都应该被定向到文件夹中的资源/static/built
我已经通过捕获所有内容NoHandlerFoundExceptions并重定向到js文件或html文件来实现这一点。然后使用 aWebMvcConfigurer来映射静态内容。
但这对我来说似乎是一种黑客行为,那么有没有一种不那么黑客的方法呢?
到目前为止,我已经为Spring引导应用程序编写了许多端点,没有html UI端。现在,我要提供包含React代码的HTML文件和js文件。
当我访问时,http://localhost:8443我得到:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Nov 19 11:54:01 PST 2017
There was an unexpected error (type=Not Found, status=404).
Not Found
Run Code Online (Sandbox Code Playgroud)
到目前为止,我做了什么:
1.添加扩展网络配置类WebMvcConfigurerAdapter:
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setPrefix("/resources/static/");
bean.setSuffix(".html");
return bean;
}
}
Run Code Online (Sandbox Code Playgroud)
2.添加了一个休息控制器端点:
@RestController
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET) …Run Code Online (Sandbox Code Playgroud) 我有一个前端带有 angular 的 Spring Boot 应用程序。
我在 html5 模式下使用 ui-router,我希望 spring 在所有未知路由上呈现相同的 index.html。
// Works great, but it also overrides all the resources
@RequestMapping
public String index() {
return "index";
}
// Seems do be the same as above, but still overrides the resources
@RequestMapping("/**")
public String index() {
return "index";
}
// Works well but not for subdirectories. since it doesn't map to those
@RequestMapping("/*")
public String index() {
return "index";
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是我怎样才能创建一个回退映射但允许通过资源?
我有一个 React 客户端应用程序,它被构建并编译到 Java 资源文件夹中
src/main/resources/static
Run Code Online (Sandbox Code Playgroud)
然后,标准 Spring Boot 应用程序将毫无问题地提供静态文件夹的内容。
但是,当我开始使用 React Router 时,我需要能够解析此路径:
localhost:8080/userSettingsPage
Run Code Online (Sandbox Code Playgroud)
进入index.html:
src/resource/static/index.html
Run Code Online (Sandbox Code Playgroud)
我知道我可以在控制器中执行此操作,如下所示:
@Controller
public class MyController {
@RequestMapping("/userSettingsPage")
public String getIndex() {
return "index.html";
}
}
Run Code Online (Sandbox Code Playgroud)
不过我想以更通用的方式指定我的控制器:
我怎样才能实现它?
spring-boot ×7
spring ×6
java ×3
spring-mvc ×3
react-router ×2
reactjs ×2
angularjs ×1
javascript ×1