2016-03-08 116 views
0

我發現了幾個問題,除了我使用的是嵌入式tomcat之外,其他問題都是相同的。沒有發現WebApplicationContext沒有在DispatcherServlet中請求,也沒有ContextLoaderListener註冊

我嘗試了所有我找到的答案,其中大部分與ContextLoaderListener和依賴關係有關。

我注意到,當我在「mvn clean package」之後構建應用程序時,有幾個關於重複的警告,但是老實說我不認爲這是問題。我相信在嵌入式Tomcat配置和上下文之間存在一些問題。層樓高

C:\STS\wsRestTemplate\TestDeployment>mvn clean package 
... 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ TestDeployment --- 
[INFO] Building jar: C:\STS\wsRestTemplate\TestDeployment\target\embeddedApp.jar 

[INFO] 
[INFO] --- appassembler-maven-plugin:1.10:assemble (default) @ TestDeployment -- 
- 
[WARNING] The usage of program name (App) is deprecated. Please use program.id i 
nstead. 
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\springframework\s 
pring-web\4.2.5.RELEASE\spring-web-4.2.5.RELEASE.jar to C:\STS\wsRestTemplate\Te 
stDeployment\target\repo\org\springframework\spring-web\4.2.5.RELEASE\spring-web 
-4.2.5.RELEASE.jar 
[INFO] Installing artifact C:\Users\e049447\.m2\repository\org\springframework\s 
pring-aop\4.2.5.RELEASE\spring-aop-4.2.5.RELEASE.jar to C:\STS\wsRestTemplate\Te 
stDeployment\target\repo\org\springframework\spring-aop\4.2.5.RELEASE\spring-aop 
-4.2.5.RELEASE.jar 
... 
[INFO] Installing artifact C:\STS\wsRestTemplate\TestDeployment\target\embeddedA 
pp.jar to C:\STS\wsRestTemplate\TestDeployment\target\repo\com\mastercard\ictt\b 
atchs\TestDeployment\0.0.1-SNAPSHOT\TestDeployment-0.0.1-SNAPSHOT.jar 
[INFO] 
[INFO] --- maven-shade-plugin:1.3.3:shade (default) @ TestDeployment --- 
[INFO] Including org.springframework:spring-web:jar:4.2.5.RELEASE in the shaded 
jar. 
[INFO] Including org.springframework:spring-aop:jar:4.2.5.RELEASE in the shaded 
...[INFO] Including commons-io:commons-io:jar:2.2 in the shaded jar. 
[INFO] Including javax.servlet:javax.servlet-api:jar:3.0.1 in the shaded jar. 
[WARNING] We have a duplicate org/apache/jasper/Constants.class in C:\Users\e049 
447\.m2\repository\org\apache\tomcat\tomcat-jasper\8.0.32\tomcat-jasper-8.0.32.j 
ar 
[WARNING] We have a duplicate org/apache/jasper/EmbeddedServletOptions.class in 
C:\Users\e049447\.m2\repository\org\apache\tomcat\tomcat-jasper\8.0.32\tomcat-ja 
sper-8.0.32.jar 
[WARNING] We have a duplicate org/apache/jasper/JasperException.class in C:\User 
s\e049447\.m2\repository\org\apache\tomcat\tomcat-jasper\8.0.32\tomcat-jasper-8. 
0.32.jar 
[WARNING] We have a duplicate org/apache/jasper/JspC.class in C:\Users\e049447\. 
m2\repository\org\apache\tomcat\tomcat-jasper\8.0.32\tomcat-jasper-8.0.32.jar 
[WARNING] We have a duplicate org/apache/jasper/JspCompilationContext.class in C 
:\Users\e049447\.m2\repository\org\apache\tomcat\tomcat-jasper\8.0.32\tomcat-jas 
per-8.0.32.jar 

Project Structure and Libs Dependencies

The error

的App-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<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.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd"> 

    <context:component-scan base-package="com.mycomp.myapp.batchs.AuthFileUpload" /> 

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

    <bean id="multipartResolver" 
       class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> 
</beans> 

的web.xml

012期間

日誌

靜態主

import org.apache.catalina.LifecycleException; 
import org.apache.catalina.startup.Tomcat; 


public class App 
{ 
    public static void main(String[] args) throws LifecycleException 
    { 
       Tomcat tomcat = new Tomcat(); 

       tomcat.setPort(8080); 
       tomcat.setBaseDir("C\\temp"); 
       tomcat.addWebapp(tomcat.getHost(), "/", "C:\\temp\\");// 
       tomcat.start(); 
       tomcat.getServer().await(); 
    } 
} 

的index.jsp(如果我的評論下面的所有線條和我添加了一個簡單的Hello字串,將工作作爲惠康頁)

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
<html> 
<body> 

<h2>Uploading a file.. </h2> 
     <form:form method="POST" action="uploadFile" enctype="multipart/form-data"> 
       Upload your file please: 
       <input type="file" name="file" /> 
       <input type="text" name="name"> 
       <input type="submit" value="Upload" /> 
       <form:errors path="file" cssStyle="color: #ff0000;" /> 
     </form:form> 

</body> 
</html> 

控制器

@Controller 
public class FileUploadController { 


     @RequestMapping(value="/upload",method = RequestMethod.GET) 
     public String printHello(ModelMap model) { 
      return "index"; 
      } 

回答

0
if your application context xml file name is App-servlet.xml then you need to pass its name as init param to the servlet 

<servlet> 
    <servlet-name>App</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/App-servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>App</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping>`enter code here` 
+0

我做到了,它保持同樣的錯誤。請注意,第7行代表「」「的錯誤點。我最好的猜測是App-servlet.xml沒有被考慮在內。請查看文件夾結構和主要方法,並讓我知道你是否認爲我錯過了某些東西。 –

1

問題是w在web.xml中使用servlet映射。根據servlet規範,映射/覆蓋容器s default servlet. This means that your DispatcherServlet maps to all requests which have no explicit mapping. However the .jsp extension is mapped to the container的jsp servlet。因此,index.jsp不會經過DispatcherServlet,因此您不能在那裏使用spring表單taglib。

,使其經過調度的servlet

@Controller 
public class WelcomeController{ 

    @RequestMapping(value = "/") 
    public String welcome(){ 
    return "index"; 
    } 
} 

那麼你的index.jsp移動到WEB-INF文件夾,以配合您的ViewResolver設置,而不是實現你的index.jsp的控制器。 然後取出

<welcome-file-list> 
       <welcome-file>index.jsp</welcome-file> 
     </welcome-file-list> 

在web.xml

+0

對不起,你告訴我的東西很有意義,但是現在當我嘗試localhost:8080時,我得到了404。在我看來,App-servlet.xml沒有考慮到。你看到有什麼遺漏了我啓動應用程序的方式(請看主要方法) –