无法基于RequestMapping值访问View

时间:2016-12-09 14:19:46

标签: database spring-mvc jdbctemplate

由于我是Spring框架的新手,我需要一些帮助来弄清楚如何将表单值直接发送到数据库,如果成功,我想显示success.html页面,如果失败,则返回fail.html页面

的pom.xml

<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>simple</groupId>
 <artifactId>simple</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
  <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
      <warSourceDirectory>WebContent</warSourceDirectory>
      <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
  </plugin>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
</plugins>
</build>
<dependencies>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.4.RELEASE</version>
</dependency>
</dependencies>
</project>

的beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
        <property name="username" value="SYSTEM"/>
        <property name="password" value="9848451415"/>
    </bean>



    <bean id="RegistrationDAO" class="com.simple.dao.RegistrationDAO">  
        <property name="dataSource" ref="dataSource"></property>  
    </bean>     


</beans>

这个RegistrationController

package simple;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;

import com.simple.services.RegistrationService;

@Controller
public class RegistrationController {

@RequestMapping(value="/registration.html",method= RequestMethod.GET)
public ModelAndView getRegistrationForm(){
    ModelAndView model = new ModelAndView("registration");
    return model;

}


@RequestMapping(value="/submitform.html",method = RequestMethod.POST)
public ModelAndView submitRegistration(@ModelAttribute("registration")   Registration registration){

    RegistrationService rs = new RegistrationService();
    int result = rs.insert(registration);

    if(result != 0){
    ModelAndView model = new ModelAndView("success");
    return model;
}
    else{
        ModelAndView model = new ModelAndView("fail");
        return model;
    }
}



}

RegistrationService

package com.simple.services;

import com.simple.dao.RegistrationDAO;

import simple.Registration;

public class RegistrationService {

public int insert(Registration r){
    RegistrationDAO rdao = new RegistrationDAO();
    return rdao.insert(r);

}

}

RegistrationDAO

package com.simple.dao;



import java.sql.Types;

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import simple.Registration;
@Component
public class RegistrationDAO {

private JdbcTemplate jdbc;  

@Autowired
public void setDataSource(DataSource ds) {  
    this.jdbc = new JdbcTemplate(ds);  
} 

public int insert(Registration r){
    int result = jdbc.update("insert into registration values(?,?,?,?)",new  Object[]{r.getFirstname(), r.getLastname(),r.getUsername(),r.getPassword()},
            new Object[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,  Types.VARCHAR});
    return result;

}

}

注册

package simple;

public class Registration {

private String firstname;
private String lastname;
private String username;
private String password;
public String getFirstname() {
    return firstname;
}
public void setFirstname(String firstname) {
    this.firstname = firstname;
}
public String getLastname() {
    return lastname;
}
public void setLastname(String lastname) {
    this.lastname = lastname;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
@Override
public String toString() {
    return "Registration [firstname=" + firstname + ", lastname=" + lastname + ", username=" + username
            + ", password=" + password + "]";
}
}

registration.html

<!DOCTYPE html>
<html>
<head>

<link href="style.css" rel="stylesheet" type="text/css" />
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>

<h2><center>Registration Page</center></h2>

<form action="" method ="get">

<p>
<center>
FirstName:&nbsp;&nbsp;&nbsp;&nbsp;<input type = "text" name = "firstname"/>
</center>
</p>
<p>
<center>
LastName:&nbsp;&nbsp;&nbsp;&nbsp;<input type = "text" name = "lastname"/>
</center>
</p>
<p>
<center>
UserName:&nbsp;&nbsp;&nbsp;&nbsp;<input type = "text" name = "firstname"/>
</center>
</p>
<p>
<center>
PassWord-:&nbsp;&nbsp;&nbsp;&nbsp;<input type = "text" name = "firstname"/>
</center>
</p>



</form>
<div id="background"><img alt="" class="stretch" src="bgcs.jpg" /></div>

</html>

Success.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>

<center><h1>Success</h1></center>
</head>
<body>

</body>
</html>

fail.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<center>
<h1>

Failure
</h1>
</center>
</head>
<body>

</body>
</html>

0 个答案:

没有答案