The requested resource is not available when wanna

2019-09-09 06:07发布

问题:

I'm using Spring mvc, and wanna make web app which send some json data to client and client should visualized them using js.

I have some questions:

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-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)

,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-How should i access to data that i created in restapi.jsp on the UserSideApp.html file?

these are some my files:

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

public class AppConfig {

@Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix("");

    return viewResolver;
} }

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";
}  }

and my both .html and .jsp files are in "/WEB-INF/views/"

回答1:

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.

Then i create a pages folder in WEB-INF and my codes changes to these:

AppInitializer became like other internet's samples.

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

@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

Creating a "resources" directory in "src/main/webapp"

add bellow cod in AppConfig.java

registry.addResourceHandler("/WEB-INF/views/resources/*.html").addResourceLocations("/resources/");

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