我是新的Spring MVC java。Servlet Spring MVC java - 不能訪問控制器和視圖
配置(我做了什麼)
- 進口彈簧庫
- 進口共記錄
- Tomcat服務器(可以訪問本地主機:8080)
問題遇到
我可以在網絡內容訪問的index.jsp沒有問題,但是當在WEB-INF,服務器顯示訪問的hello.jspHTTP狀態404,網址在http://localhost:8080/APK_downloader/WEB-INF/jsp/hello.jsp
停止的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>APK downloader</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
彈簧調度-servlet.xml中(servlet配置XML)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.3.xsd">
<context:component-scan base-package="solutionView"/>
<bean id="HanlderMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
DBController.java(java的資源類)
package solutionView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = "/hello")
public class DBController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public ModelAndView getModelView(){
System.out.println("helllo"); // check point
return new ModelAndView("hello");
}
}
的hello.jsp(視圖JSP)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello</h1>
<h2>${message}</h2>
</body>
</html>
您不能訪問WEB-INF文件直接。嘗試到訪問http://本地主機:8080/APK_downloader /你好 –
請仔細閱讀本[問題](http://stackoverflow.com/questions/25904298/why -jsp-files-inside-web-inf-folder-works-but-under-a-folder-under-web),它會幫助你很多。 –
@rupesh_padhye我不想用url訪問,有沒有通過控制器訪問jsp的方法? –