无法解析符号'ModelAndView'

时间:2016-11-08 19:07:42

标签: java spring maven spring-mvc pom.xml

我是spring框架的新手,我正在尝试使用JPA构建MVC应用程序。 在我的UserController.java文件中,我收到此错误: Error:(12, 39) java: package org.springframework.web.portlet does not exist

Error:(45, 29) java: cannot find symbol
symbol:   class ModelAndView
location: class xxx..UserController

我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>xxx</groupId>
    <artifactId>springmvcwithjpa</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>xxx</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring Dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>1.4.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.4.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.31</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>1.4.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>


    </dependencies>

</project>

UserController.java

package xxx.controllers;

import xxx.Address;
import xxx.IUserDAO;
import xxx.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserDAO userDAO;

    @RequestMapping("*")
    public String getAddUserPage(HttpServletRequest request) {
        return "adduser";
    }

    @RequestMapping(value ="/{userId}",method= RequestMethod.GET)
    public String getUser(@PathVariable("userId") int userId,
                          ModelMap model) {
        System.out.println("userId requested is "+userId);
        model.addAttribute("userId", userId);
        return "viewuser";
    }

    @RequestMapping(value ="/{userId}",method= RequestMethod.POST)
    public ModelAndView add(@PathVariable("userId") String userId,
                            @PathVariable("firstName") String firstName,
                            @PathVariable("lastName") String lastName,
                            @PathVariable("title") String title,
                            @PathVariable("address") Address address,
                            ModelAndView modelAndView) {
        try {
            User newUser = new User(userId, firstName, lastName, title, address);
            String newuserId = userDAO.saveUserDetails(newUser);
            return new ModelAndView("redirect:/user/" + newuserId);
        } catch (Exception e) {
            e.printStackTrace();
            modelAndView.setViewName("error");
            modelAndView.addObject("message",
                    "500 Internal Server Error : There was an error Processing your Request!!");
            return modelAndView;
        }

    }
}

我在我的pom.xml中添加了spring和spring-mvc所需的所有依赖项。 我正在尝试在jdk 1.7上构建这个程序。 知道我哪里错了吗? 帮助将不胜感激。谢谢。

0 个答案:

没有答案