简体   繁体   English

JSP和HTML之间的Spring Servlet

[英]Spring servlet between jsp and html

I wanna know how to servlet call html and jsp file page. 我想知道如何servlet调用html和jsp文件页面。 I'm using spring mvc with servlet 我正在使用带有servlet的spring mvc

when I run 当我跑步时

localhost:8080/ -> it's running correctly 本地主机:8080 /->它运行正常

localhost:8080/htmlPage -> it's not working return error 404 not found 本地主机:8080 / htmlPage->无法正常工作,返回错误404

Here my path 这是我的路

在此处输入图片说明

Here my code 这是我的代码

servlet mvc-dispatcher-servlet.xml servlet mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.springapp.mvc"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=""/>
</bean>

<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:annotation-driven />

web.xml web.xml中

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

HelloController.java HelloController.java

@Controller
public class HelloController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String wellcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "hello.jsp";
    }

    @RequestMapping(value="/htmlPage", method = RequestMethod.GET )
    public String startHtml(){
        return "hello.html";
    }

}

Your call is wrong. 你打错了 First there should be an app name and then request name. 首先应该有一个应用名称,然后是请求名称。
You have to call like below : 您必须像下面这样打电话:

localhost:8080/YourAppName/htmlPage
    <web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*.so</url-pattern>
</servlet-mapping>

@Controller
public class HelloController {

    @RequestMapping(value = "/home.so", method = RequestMethod.GET)
    public String wellcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "hello.jsp";
    }

    @RequestMapping(value="/htmlPage.so", method = RequestMethod.GET )
    public String startHtml(){
        return "hello.html";
    }
}

please change your url pattern with " .so". 请使用“ .so”更改网址格式 all " .so" request are consider as spring request and handle by spring dispatcher servlet. 所有“ .so”请求都将被视为Spring请求,并由Spring Dispatcher Servlet处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM