0

I am new to spring framework and I am trying to build a MVC application using JPA. In my UserController.java file,I get this error: 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

My 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;
        }

    }
}

I have added all the required dependencies for spring and spring-mvc in my pom.xml . I am trying to build this program on jdk 1.7. Any idea where am I going wrong? Help would be appreciated. Thanks.

learntogrow-growtolearn
  • 1,190
  • 5
  • 13
  • 37

0 Answers0