我尝试使用Maven执行Spring MVC项目,但在maven打包时遇到编译错误 -
错误
[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";
}
}
项目结构
[pr@web449 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
答案 0 :(得分:3)
添加导入语句
import org.springframework.web.bind.annotation.PathVariable;
答案 1 :(得分: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>
确保您拥有 spring-web jar。
如果你检查org.springframework.web.bind.annotation.PathVariable
所在的位置,你会发现它在上面的jar中:
http://mvnrepository.com/artifact/org.springframework/spring-web/3.0.4.RELEASE
当然,jar版可能会有所不同,只是确保使用你的版本。你可以在这里找到spring-web版本:
http://mvnrepository.com/artifact/org.springframework/spring-web
正如Reimeus指出他的回答,你也需要导入。