要映射到.html文件时,请求的资源不可用 [英] The requested resource is not available when wanna map to .html file
问题描述
我正在使用Spring mvc,并且想要制作向客户端发送一些json数据的网络应用,客户端应该使用js对其可视化.
I'm using Spring mvc, and wanna make web app which send some json data to client and client should visualized them using js.
我有一些问题:
1-我的项目在* .jsp文件旁边有一些* .html,如果没有web.xml,如何处理这两个问题.我编写的代码可以在* .jsp上正常工作,但是给出请求的资源不可用".映射html文件时出错.
1-My project have some *.html beside *.jsp file how can I handle both without web.xml. the code that i had written work fine with *.jsp but give "The requested resource is not available." error for mapping html files.
2-正如我所说,当我想使用以下代码解析* .jsp文件中服务器上的json字符串时,我的服务必须以json形式向客户端发送对象列表
2-As i said My service have to send a list of object in json form to client, when i want to parse the json string on server in *.jsp file with the code like
MyClass data = new Gson().fromJson(MyList.get(0).toString(), listType)
,我遇到很多问题,所以我决定在* .html文件的客户端中执行此工作,我想知道当我不希望该客户端知道有关该客户端的信息时该如何处理该解析工作.安排我的课程?或者plz告诉我是否必须与客户端共享我的课程,我应该如何将其结构发送到html文件?
,I face with to many problems, so i decide to do this job in client Side in *.html file, i want to know how should i handle this parsing job when i don't want that client know about the the structure my class? OR plz tell if have to share my class with client, how should i send stucture of it to html file?
3-我应该如何访问在UserSideApp.html文件的restapi.jsp中创建的数据?
3-How should i access to data that i created in restapi.jsp on the UserSideApp.html file?
这些是我的一些文件:
AppInitializer.java
AppInitializer.java
public class AppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet(
"dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
} }
AppConfig.java
AppConfig.java
public class AppConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix("");
return viewResolver;
} }
我的AppContoller.java部分
the part of my AppContoller.java
@Controller
@RequestMapping("/")
public class AppController {
@Autowired
HackDataService service;
@RequestMapping(value = "/restapi", method = RequestMethod.GET)
public String jsonAPI(ModelMap model) {
List<HackData> newList = service.findAllNewData();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json="";
try {
json = ow.writeValueAsString(newList);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
model.addAttribute("List", json);
String newjson = new Gson().toJson(newList);
model.addAttribute("newList", newjson);
return "newTest.jsp";
}
@RequestMapping(value = "/app", method = RequestMethod.GET)
public String htmlapp() {
return "UserSideApp.html";
} }
我的.html和.jsp文件都在"/WEB-INF/views/"中
and my both .html and .jsp files are in "/WEB-INF/views/"
推荐答案
在第一个问题上花了将近一天的时间后,我找到了答案, 首先,我没有实现WebApplicationInitializer,而是扩展了AbstractAnnotationConfigDispatcherServletInitializer,对于Java配置,我扩展了WebMvcConfigurerAdapter.
After spending almost a day on first problem, i find the answer of it, first of all instead of implementing WebApplicationInitializer, I extend AbstractAnnotationConfigDispatcherServletInitializer and for java Configing i extends WebMvcConfigurerAdapter.
然后我在WEB-INF中创建一个页面文件夹,我的代码更改为以下内容:
Then i create a pages folder in WEB-INF and my codes changes to these:
AppInitializer变得像其他互联网示例一样.
AppInitializer became like other internet's samples.
AppConfig.java
AppConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.attackmap" })
public class AppConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/WEB-INF/pages/");
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix("");
return viewResolver;
}
}
AppController.java
AppController.java
@Controller
@RequestMapping("/")
public class AppController {
@RequestMapping(value = { "/", "/info" }, method = RequestMethod.GET)
public String welcome(ModelMap model) {
model.addAttribute("message", "home page with info about site");
return "homepage.jsp";
}
@RequestMapping(value = "/finalapp", method = RequestMethod.GET)
public String test() {
return "redirect:/pages/final.htm";
} }
还有另一种方式
there was another way
在"src/main/webapp"中创建资源"目录
Creating a "resources" directory in "src/main/webapp"
在AppConfig.java中添加下面的鳕鱼
add bellow cod in AppConfig.java
registry.addResourceHandler("/WEB-INF/views/resources/*.html").addResourceLocations("/resources/");
对于viewResolve,请使用以下代码:
and for viewResolving use the code like bellow:
@RequestMapping(value = "/newapp", method = RequestMethod.GET)
public String test2() {
return "/resources/UserSideAppNew.html";
}
但我的其他两个问题仍未解决,主要问题是第一个,但是如果您知道……关于这两个问题,请告诉我有关他们的问题
but still my two other questions are unsolved, the main problem was first one but if you know sth, about these two plz tell me about them
这篇关于要映射到.html文件时,请求的资源不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!