@Pathvariable没有找到编译错误
问题描述:
我试着使用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
答
添加import语句
import org.springframework.web.bind.annotation.PathVariable;
答
我认为你在类路径中缺少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在他的回答指出,你所需要的进口所。