转载

【java实例】自己动手实现IOC和MVC(八)

来对上一节实现的mvc做个测试吧 首先dao层PersonDao.Java.这里做一些假数据
  1. package com.ajun.ts.dao;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.ajunframework.beans.annotation.Dao;
  5. @Dao
  6. public class PersonDao{
  7.     public List<String> findAll(){
  8.         List<String> list = new ArrayList<String>();
  9.         list.add("zhao");
  10.         list.add("jun");
  11.         list.add("liu");
  12.         list.add("zhen");
  13.         return list;
  14.     }
  15. }
 
service层PersonService.java
  1. package com.ajun.ts.service;
  2. import java.util.List;
  3. import com.ajun.ts.dao.PersonDao;
  4. import com.ajunframework.beans.annotation.Property;
  5. import com.ajunframework.beans.annotation.Service;
  6. @Service
  7. public class PersonService {
  8.     @Property
  9.     private PersonDao personDao;
  10.     public List<String> findAll(){
  11.         return personDao.findAll();
  12.     }
  13.     public PersonDao getPersonDao() {
  14.         return personDao;
  15.     }
  16.     public void setPersonDao(PersonDao personDao) {
  17.         this.personDao = personDao;
  18.     }
  19. }
action层PersonAction.java
  1. package com.ajun.ts.controller;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import com.ajun.ts.service.PersonService;
  6. import com.ajunframework.beans.annotation.Action;
  7. import com.ajunframework.beans.annotation.Property;
  8. import com.ajunframework.servlet.annotation.RequestMapping;
  9. import com.ajunframework.servlet.view.View;
  10. import com.ajunframework.servlet.view.ViewData;
  11. @Action
  12. public class PersonAction {
  13.     @Property
  14.     private PersonService personService;
  15.     @RequestMapping("person/list")//跳转路径
  16.     public View personList(HttpServletRequest request, HttpServletResponse response){
  17.         ViewData view = new ViewData();
  18.         List<String> list = personService.findAll();
  19.         view.put("list", list);//此处就是设置数据在request范围内了
  20.         return new View("/jsp/person/list.jsp");
  21.     }
  22.     public PersonService getPersonService() {
  23.         return personService;
  24.     }
  25.     public void setPersonService(PersonService personService) {
  26.         this.personService = personService;
  27.     }
  28. }
配置注入的扫描的包constant.properties
  1. scan-package=com.ajun.ts//这个包先必须在classpath下,没有做路径的处理
配置DispatchServlet在web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3.     xmlns="http://java.sun.com/xml/ns/javaee"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7.   <servlet>
  8.     <servlet-name>DispatchServlet</servlet-name>
  9.     <servlet-class>com.ajunframework.servlet.DispatchServlet</servlet-class>
  10.   </servlet>
  11.   <servlet-mapping>
  12.     <servlet-name>DispatchServlet</servlet-name>
  13.     <url-pattern>*.ajun</url-pattern>
  14.   </servlet-mapping>
  15.   <welcome-file-list>
  16.     <welcome-file>index.jsp</welcome-file>
  17.   </welcome-file-list>
  18. </web-app>
页面请求
  1. <a href="person/list.ajun">LSIT PERSON</a>
展现页面
  1. <table border="1" align="center" width="80%">
  2.         <tr>
  3.             <td>Name</td>
  4.         </tr>
  5.         <c:forEach items="${list}" var="p">
  6.             <tr>
  7.                 <td>${p}</td>
  8.             </tr>
  9.         </c:forEach>
  10.     </table>
好了 ,至此整个自己动手实现IOC和MVC完成了 ,希望对大家有帮助
正文到此结束
Loading...