如何将这些文件安排到Spring MVC结构中

时间:2013-07-31 11:10:54

标签: spring-mvc

这是MainApp.java

package spring.pac;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
 public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");

      StudentJDBCTemplate studentJDBCTemplate = 
      (StudentJDBCTemplate)context.getBean("studentJDBCTemplate");

      System.out.println("------Records Creation--------" );
      studentJDBCTemplate.create("Zara", 11);
      studentJDBCTemplate.create("Nuha", 2);
      studentJDBCTemplate.create("Ayan", 15);

      System.out.println("------Listing Multiple Records--------" );
      List<Student> students = studentJDBCTemplate.listStudents();
      for (Student record : students) {
         System.out.print("ID : " + record.getId() );
         System.out.print(", Name : " + record.getName() );
         System.out.println(", Age : " + record.getAge());
      }

      System.out.println("----Updating Record with ID = 2 -----" );
      studentJDBCTemplate.update(2, 20);

      System.out.println("----Listing Record with ID = 2 -----" );
      Student student = studentJDBCTemplate.getStudent(2);
      System.out.print("ID : " + student.getId() );
      System.out.print(", Name : " + student.getName() );
      System.out.println(", Age : " + student.getAge());

   }
}

Student.java

package spring.pac;

public class Student {
private Integer age;
   private String name;
   private Integer id;

   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      return age;
   }

   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }

   public void setId(Integer id) {
      this.id = id;
   }
   public Integer getId() {
      return id;
   }
}

StudentDAO.java

package spring.pac;

import java.util.List;

import javax.sql.DataSource;

public interface StudentDAO {

   public void setDataSource(DataSource ds);

   public void create(String name, Integer age);

   public Student getStudent(Integer id);

   public List<Student> listStudents();

   public void delete(Integer id);

   public void update(Integer id, Integer age);
}

StudentJDBCTemplate.java

package spring.pac;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

public class StudentJDBCTemplate {
 private DataSource dataSource;
   private JdbcTemplate jdbcTemplateObject;

   public void setDataSource(DataSource dataSource) {
      this.dataSource = dataSource;
      this.jdbcTemplateObject = new JdbcTemplate(dataSource);
   }

   public void create(String name, Integer age) {
      String SQL = "insert into Student (name, age) values (?, ?)";

      jdbcTemplateObject.update( SQL, name, age);
      System.out.println("Created Record Name = " + name + " Age = " + age);
      return;
   }

   public Student getStudent(Integer id) {
      String SQL = "select * from Student where id = ?";
      Student student = jdbcTemplateObject.queryForObject(SQL, 
                        new Object[]{id}, new StudentMapper());
      return student;
   }

   public List<Student> listStudents() {
      String SQL = "select * from Student";
      List <Student> students = jdbcTemplateObject.query(SQL, 
                                new StudentMapper());
      return students;
   }

   public void delete(Integer id){
      String SQL = "delete from Student where id = ?";
      jdbcTemplateObject.update(SQL, id);
      System.out.println("Deleted Record with ID = " + id );
      return;
   }

   public void update(Integer id, Integer age){
      String SQL = "update Student set age = ? where id = ?";
      jdbcTemplateObject.update(SQL, age, id);
      System.out.println("Updated Record with ID = " + id );
      return;
   }
}

StudentMapper.java

package spring.pac;

import java.sql.ResultSet;

import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

public class StudentMapper implements RowMapper<Student> {

      public Student mapRow(ResultSet rs, int rowNum) throws SQLException {

          Student student = new Student();

          student.setId(rs.getInt("id"));

          student.setName(rs.getString("name"));

          student.setAge(rs.getInt("age"));

          return student;
   }
}

我正在使用JBoss 5和eclipse。请帮我一个是Model,哪一个是View,哪一个是Controller

1 个答案:

答案 0 :(得分:0)

乍一看,这不是一个春季MVC项目。 要遵守Spring MVC,您应该使用@Controller注释一个Controller类。而且,我看不到任何可以作为视图的jsp页面或类似页面。

总结一下Spring MVC元素:

<强>控制器:  它是一个接收http请求并触发相应业务逻辑的组件。

<强>模型:  它只是表示传递给视图的域对象的一堆数据

查看:  数据的处理方式。

这里有一个例子可以帮助您获得概念。假设您有一个Web应用程序,允许您在数据库中搜索学生。通过输入学生姓名并点击“搜索”按钮,您可以向服务器发送http请求(可能是POST)并等待响应。因此,Web应用程序有一个控制器类,例如StudentsController,用于管理有关学生的请求。 StudentsController公开绑定到URL http://.../student/search的方法searchStudent(...)。这意味着当您向此URL发送请求时,将执行该方法。 好的,我们假设您在请求正文中传递了所需的参数,以便按名称检索学生。在searchStudent(...)方法内部,会发生一些业务逻辑来从数据库中检索学生。可能以这种方式获取的信息将表示为您要作为html页面发送给客户端的Student对象。

为此,您将把学生发送到一个jsp,它在一些html标签内格式化Student类中的数据。传递给jsp的Student类是模型,jsp本身是视图

您还可以将学生数据以json格式发送给某个客户。学生的json表示也是一种观点。