2016-03-08 114 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没有考虑到。你看到有什么遗漏了我启动应用程序的方式(请看主要方法) –