2014-08-28 126 views
0

我試着使用Maven執行Spring MVC的項目,但同時得到行家包裝編譯錯誤 -@Pathvariable沒有找到編譯錯誤

錯誤

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:compile (default-compile) on project CounterWebApp: Compilation failure 
[ERROR] /home/prem1980/apache-maven/all_maven_projects/java_webapp_project/CounterWebApp/src/main/java/com/mkyong/controller/BaseController.java:[23,36] cannot find symbol 
[ERROR] symbol : class PathVariable 
[ERROR] location: class com.mkyong.controller.BaseController 

的java文件

package com.mkyong.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
@RequestMapping("/") 
public class BaseController { 

     @RequestMapping(value="/welcome", method = RequestMethod.GET) 
     public String welcome(ModelMap model) { 

       model.addAttribute("message", "Maven Web Project + Spring 3 MVC - welcome()"); 

       //Spring uses InternalResourceViewResolver and return back index.jsp 
       return "index"; 

     } 

     @RequestMapping(value="/welcome/{name}", method = RequestMethod.GET) 
     public String welcomeName(@PathVariable String name, ModelMap model) { 

       model.addAttribute("message", "Maven Web Project + Spring 3 MVC - " + name); 
       return "index"; 

     } 

} 

項目結構

[[email protected] CounterWebApp]$ tree . 
. 
├── pom.xml 
├── src 
│   └── main 
│    ├── java 
│    │   └── com 
│    │    └── mkyong 
│    │     └── controller 
│    │      └── BaseController.java 
│    ├── resources 
│    └── webapp 
│     └── WEB-INF 
│      ├── index.jsp 
│      ├── mvc-dispatcher-servlet.xml 
│      └── web.xml 
└── target 
    ├── classes 
    ├── generated-sources 
    │   └── annotations 
    └── maven-status 
     └── maven-compiler-plugin 
      └── compile 
       └── default-compile 
        └── createdFiles.lst 

回答

1

添加import語句

import org.springframework.web.bind.annotation.PathVariable; 
1

我認爲你在類路徑中缺少spring-web jar。 spring-web jar包含該註釋。

確保你的pom.xml包含:

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-web</artifactId> 
    <version>3.0.4.RELEASE</version> 
</dependency> 

確保您有彈簧網罐子。

如果選中其中org.springframework.web.bind.annotation.PathVariable位於你會發現,這是在上述範圍內的jar:

http://mvnrepository.com/artifact/org.springframework/spring-web/3.0.4.RELEASE

當然罐子的版本可能不同,只是確保使用你的。你可以在這裏找到春天的Web版本:

http://mvnrepository.com/artifact/org.springframework/spring-web

而作爲Reimeus在他的回答指出,你所需要的進口所。