我正在开发spring-mvc web应用程序,我遇到了一些映射问题: 我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>pages/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
我的控制器:
@Controller
public class MainController {
@Autowired
UserService userService;
@Autowired
PhotosService photosService;
@RequestMapping(method=RequestMethod.GET)
public String loadIndex(Model model)
{
model.addAttribute("firstName", "WWWALTER");
return "index";
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String save( @ModelAttribute("document") PhotosEntity photosEntity,
@RequestParam("file") MultipartFile file) {
Blob blob = null;
try {
blob = new SerialBlob(file.getBytes());
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// PhotosEntity photosEntity = new PhotosEntity();
photosEntity.setContent(blob);
photosEntity.setFilename(file.getOriginalFilename());
photosService.saveFile(photosEntity);
return "index";
}
此外,我在tomcat 7中有一个应用程序名称 - &#34; c2&#34;。所以我应该用&#34; c2 /&#34;前缀?
你能帮我创建正确的映射吗? 我想在第一个方法中添加一个属性,并在jsp页面中检索它:
@RequestMapping(method=RequestMethod.GET)
public String loadIndex(Model model)
{
model.addAttribute("firstName", "WWWALTER");
return "index";
}
- 但是这个方法没有被调用。
答案 0 :(得分:0)
您可以从jsp-page中检索如下所示的属性:${firstName}
。
还可以尝试将@RequestMapping(method=RequestMethod.GET)
更改为@RequestMapping(value = "/", method=RequestMethod.GET)
答案 1 :(得分:0)
这听起来很愚蠢。但我只是忘了把
<mvc:annotation-driven />
在servlet上下文文件中。遗憾!