2013-07-31 31 views
0

這裏是MainApp.java我如何安排這些文件到Spring MVC結構

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和月食。請幫助我哪一個是Model,哪一個是View和哪一個是Controller

回答

0

乍一看這不是一個春天的MVC項目。 爲了遵守Spring MVC,你應該有一個用@Controller註解的Controller類。而且,我看不到任何可以作爲視圖的jsp頁面或類似內容。

只是爲了總結彈簧MVC元素:

控制器: 它是接收HTTP請求並觸發相應的業務邏輯的部件。

型號: 它只不過是一羣代表傳遞給視圖中的數據的domainObject的

觀點: 數據redered的方式。

在這裏你有一個例子來幫助你獲得這個概念。假設您有一個Web應用程序,它允許您在數據庫中搜索學生。通過灌輸學生姓名並點擊「搜索」按鈕,向服務器發送一個http請求(可能是POST)並等待響應。因此,該Web應用程序有一個控制器類,例如StudentsController,它管理有關學生的請求。 StudentsController公開了一種方法searchStudent(...),該方法綁定到URL http://.../student/search。這意味着當您向該URL發送請求時,該方法將被執行。 好吧,讓我們假設你在請求主體中傳入了所需的參數以按名稱檢索學生。在searchStudent(...)方法裏面發生了一些業務邏輯從db中檢索學生。可能以這種方式獲取的信息將表示爲要以HTML頁面發送給客戶端的Student對象。

爲此,您將發送Student到一個jsp,該jsp格式化來自某些html標籤內的Student類的數據。這裏傳遞給jsp的Student類是模型,而jsp本身是視圖

您也可以將json格式的學生數據發送給某個客戶端。學生的json表示也是一種觀點。

+0

沒有使用Spring MVC可以使用這些類來渲染視圖中的值。請告訴我詳細的過程.. –

+0

沒有。StudentMapper,StudentJDBCTemplate和StudentDAO是關於持久性的。學生是領域對象,主要做什麼,但與數據庫交互,首先創建三名學生,然後提取他們並打印他們的屬性。這裏有** NO SERVER **。 **根本沒有MVC **。 – MaVVamaldo

相關問題