熟悉的代码:
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
我的理解是/*地图http://host:port/context/*.
怎么样/?它肯定不会映射到http://host:port/contextroot.事实上,它会接受http://host:port/context/hello,但拒绝http://host:port/context/hello.jsp.
任何人都可以解释如何http://host:port/context/hello映射?
我想要一些混凝土过滤器应用于除一个混凝土之外的所有网址(即/*除了/specialpath).
有可能这样做吗?
示例代码:
<filter>
<filter-name>SomeFilter</filter-name>
<filter-class>org.somproject.AFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SomeFilter</filter-name>
<url-pattern>/*</url-pattern> <!-- the question is: how to modify this line? -->
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud) 我正在使用Spring开发一个网站,并且我正在尝试提供不是.jsp文件的资源(例如.html)
现在我已经注释掉了我的servlet配置的这一部分
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
Run Code Online (Sandbox Code Playgroud)
并试图从控制器返回资源的完整路径.
@Controller
public class LandingPageController {
protected static Logger logger = Logger.getLogger(LandingPageController.class);
@RequestMapping({"/","/home"})
public String showHomePage(Map<String, Object> model) {
return "/WEB-INF/jsp/index.html";
}
}
Run Code Online (Sandbox Code Playgroud)
index.html文件存在于该文件夹中.
注意:当我将index.html更改为index.jsp时,我的服务器现在正确地提供页面.
谢谢.
我要做的是将请求映射到servlet根目录(正确的术语?).我正处于将URL映射到正确视图的位置,但无法找到作为页面一部分的所有静态内容--css,javascript,images.
所以在我的web.xml中,我的servlet标签看起来像这样
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
我的控制器看起来像这样:
@RequestMapping("/shop")
public class TheShopController extends MyBaseController {
public static String VIEW = "Tile.Shop";
@Override
@RequestMapping(method = RequestMethod.GET)
protected ModelAndView processRequest(HttpServletRequest req, HttpServletResponse resp) {
ModelAndView mav = new ModelAndView(VIEW);
return mav;
}
}
Run Code Online (Sandbox Code Playgroud)
MyBaseController非常简单.它看起来像这样:
public abstract class MyBaseController extends AbstractController {
protected Logger log = Logger.getLogger(getClass());
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse resp)
throws Exception {
ModelAndView mav = processRequest(req, resp);
return mav;
}
protected abstract ModelAndView processRequest(HttpServletRequest req, HttpServletResponse resp);
}
Run Code Online (Sandbox Code Playgroud)
我在视图层中使用Tiles.我的配置如下: …
基本配置文件看起来不直观.
如果我创建简单的hello world示例,然后重命名home.jsp为home.html和编辑servlet-context.xml文件
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
Run Code Online (Sandbox Code Playgroud)
至
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".html" />
</beans:bean>
Run Code Online (Sandbox Code Playgroud)
我开始出错了
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myapp/WEB-INF/views/home.html] in DispatcherServlet with name 'appServlet'
Run Code Online (Sandbox Code Playgroud)
为什么?什么suffix属性意味着?
UPDATE
我的控制器如下.如您所见,它不包含文件扩展名
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its …Run Code Online (Sandbox Code Playgroud) 我在项目中添加了一个外部CSS样式表,并放在Eclipse项目的WEB-CONTENTS文件夹中.当我在Tomcat上部署它时,没有应用样式表.当我在Chrome中调试并打开它时,它给了我404 file not found错误.为什么这样以及如何解决?
这是代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>joined now </title>
<link href="globalCSS.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div>this is at the top</div>
<c:import url="header.jsp" />
<c:import url="navigationBar.jsp" />
<c:import url="leftpane.jsp" />
<c:import url="mainContent.jsp" />
<c:import url="rightpane.jsp" />
<c:import url="footer.jsp" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我使用Netbeans创建了一个Spring MVC 3.0应用程序.我有一个简单的控制器和JSP视图.除了不呈现的图像之外,JSP视图正确显示.我的目录结构如下所示:

在我的Home.jsp页面中,未呈现的图像被引用如下:
<img src="Images/face.png" />
Run Code Online (Sandbox Code Playgroud)
我已经验证face.png位于Images目录中.那么为什么它不会出现在浏览器中呢?在Spring MVC中,我应该在哪里放置JSP视图引用的文件,如图像,CSS,JS等?
我创建了MVC应用程序.
我想将js或css文件包含到jsp中.
我的静态文件在:
- webapp
-js/jquery.js
-WEB-INF|
|
- jsp/*.jsp
我包含jquery的代码是:
<script type="text/javascript" src="<c:url value="js/jquery.js" />"></script>
Run Code Online (Sandbox Code Playgroud)
我无法将js文件加载到视图中.
我看到包含信息的日志:
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/pool/js/jquery.js] in DispatcherServlet with name 'appServlet'
Run Code Online (Sandbox Code Playgroud)
是什么意思,MVC尝试将url映射到js文件.
我认为我的配置有一些东西,但我不知道是什么.
我的web.xml是:
<?xml version="1.0" encoding="UTF-8"?>
Run Code Online (Sandbox Code Playgroud)
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application …Run Code Online (Sandbox Code Playgroud) 我收到了这个错误.
我的web.xml有这个
<servlet>
<servlet-name>springweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springweb</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
我在我的web-application-config.xml中有这个
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
<bean name="/Scheduling.htm" class="com.web.SchedulingController"/>
Run Code Online (Sandbox Code Playgroud)
我的com.web.SchedulingController看起来像这样
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class SchedulingController implements Controller{
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView("/jsp/Scheduling_main.jsp");
modelAndView.addObject("message","Hello World MVC!!"); …Run Code Online (Sandbox Code Playgroud) 在我的Spring Boot应用程序中,我有以下静态文件:
\src\main\resources\static\images\social\facebook\f_logo.jpg
Run Code Online (Sandbox Code Playgroud)
这是我的application.properties
server.port: 8080
server.contextPath: /api
Run Code Online (Sandbox Code Playgroud)
我正在尝试通过以下网址访问此文件:
http:// localhost:8080 / api / images / social / facebook / f_logo.jpg
但是服务器返回 404 Not Found.
我在做什么错以及如何解决?
更新
我的pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<name>example</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jackson.version>2.8.0</jackson.version>
<spring.version>4.3.5.RELEASE</spring.version>
<spring.boot.version>1.4.3.RELEASE</spring.boot.version>
<cdi-api.version>2.0-EDR1</cdi-api.version>
<slf4j.version>1.7.18</slf4j.version>
<logback.version>1.1.6</logback.version>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<commons-lang3.version>3.4</commons-lang3.version>
<commons-validator.version>1.5.0</commons-validator.version>
<commons-io.version>2.4</commons-io.version>
<jacoco-maven-plugin.version>0.7.4.201502262128</jacoco-maven-plugin.version>
<maven-surefire-plugin.version>2.19</maven-surefire-plugin.version>
<lifecycle-mapping.version>1.0.0</lifecycle-mapping.version>
<java.source.version>1.8</java.source.version>
<java.target.version>1.8</java.target.version>
</properties>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>neo4j-release-repository</id>
<name>Neo4j Maven 2 release repository</name>
<url>http://m2.neo4j.org/content/repositories/releases</url>
<releases> …Run Code Online (Sandbox Code Playgroud) spring ×4
spring-mvc ×4
java ×3
servlets ×3
jsp ×2
assets ×1
el ×1
jstl ×1
spring-boot ×1
tiles ×1
url-pattern ×1
web.xml ×1