Spring MVC中不会激怒控制器类

时间:2019-07-11 01:48:10

标签: spring-mvc jpa

Spring MVC Controller类无法正常工作并抛出404。

在应用程序中,我创建了一个实体,并添加了MS-SQL数据库的所有凭据。但是,在启动该应用程序并单击“提交”按钮后,该应用程序将降落到404,并且不会触发控制器,也不会创建实体。

index.jsp如下:

 <form action = "/save" method = "post">
     ID: <input type = "text" name = "uid">
     <br />
     Name: <input type = "text" name = "name" />
     <input type = "submit" value = "Submit" />
  </form>

控制器类如下:

@Controller
public class MyController {

    @Autowired
    public EmployeeDao empDao;

    @RequestMapping("/save")
    @ResponseBody
    public String save(Employee emp){
        System.out.println("Here");
        empDao.saveEmployee(emp);
        return "success";
    }
}

实体类:

Entity
@Table(name="employee")

public class Employee {
    @Id
    private int uid;
    private String name;
}

上下文类:

@Bean
    public DriverManagerDataSource getDataSource() {

        DriverManagerDataSource ds = new DriverManagerDataSource();

        ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        ds.setUrl("jdbc:sqlserver://localhost:1433;databasename=cruddb11;integratedsecurity=true");
        ds.setUsername("ABCD");
        ds.setPassword("00000");

        return ds;
    }

Pom:

<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>org.websparrow</groupId>
    <artifactId>spring-mvc-fetch-data</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <!-- Hibernate Core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.17.Final</version>
        </dependency>

        <!-- Hibernate Validator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.1.0.Alpha5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.2.Final</version>
        </dependency>

        <!-- spring mvc dependency -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!-- spring jdbc dependency -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>6.2.2.jre8</version>
        </dependency>

        <!-- mysql databse connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>


    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2 个答案:

答案 0 :(得分:0)

这将无法正常工作。

1。)这不是必需的,但如果您的网址中有多个前导部分,则很有用。用@RequestMapping注释为您的类添加注释。像这样:

@RequestMapping(value="/fileoperations")    // Then you don't need the leading slash in the method
@Scope(value="session")

如果您正在使用会话,请使用第二行Tomcat。

2。)该表单仅包含变量uid和name,因此您必须将它们作为参数查询才能在控制器中使用。

3。)而且,您还必须告诉您的控制器使用正确的Request方法。像这样:

@RequestMapping(value="save", method=RequestMethod.POST)
public @ResponseBody String save(
        @RequestParam String uid,
        @RequestParam String name) {

      System.out.println("Here");

      // Maybe you should load the Employee first before updating it.
      Employee emp = this.empDao.readEmployee(name);
      if (emp != null) {
        emp.setUID(uid);
        emp.setName(name);
        this.empDao.saveEmployee(emp);
        return "success";
      } else {
        return "error: no employee here by that name";
      }

}

答案 1 :(得分:-1)

将您的save方法签名更改为以下内容,并用@ModelAttribute注释Employee参数

@Autowired
public EmployeeDao empDao;

@RequestMapping("/save")
@ResponseBody
public String save(@ModelAttribute Employee emp){
    System.out.println("Here");
    empDao.saveEmployee(emp);
    return "success";
}

您最好将@RequestMapping更改为@Post