问题描述
我想知道如何servlet调用html和jsp文件页面。 我正在使用带有servlet的spring mvc
当我跑步时
本地主机:8080 /->它运行正常
本地主机:8080 / htmlPage->无法正常工作,返回错误404
这是我的路
这是我的代码
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-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
@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";
}
}
1楼
你打错了
首先应该有一个应用名称,然后是请求名称。
您必须像下面这样打电话:
localhost:8080/YourAppName/htmlPage
2楼
<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";
}
}
请使用“ .so”更改网址格式。 所有“ .so”请求都将被视为Spring请求,并由Spring Dispatcher Servlet处理。