也就是不寫dwr.xml,用annotation的方式一樣可以
以前端取資料庫的 method為例,改成annotation
※web.xml
<servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>classes</param-name> <param-value> dao.impl.DaoImpl, vo.Chess </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
※只有增加classes的init-param部分
※這裡增加impl和java bean,如果不寫會在執行時出「DefaultConverterManager convertOutbound」的錯
※impl
@RemoteProxy(name="dao") public class DaoImpl implements Idao { @Override @RemoteMethod public boolean save(Chess chess) { System.out.println("新增成功!"); return true; } @Override @RemoteMethod public Chess getChess(Long id) { Chess chess = new Chess(); chess.setId(id); chess.setName("象棋"); chess.setDate(new Date()); return chess; } @Override @RemoteMethod public List<Chess> getAllChess() { List<Chess> chList = new ArrayList<>(); for (long i = 1; i < 4; i++) { Chess chess = new Chess(); chess.setId(i); if (i == 1) { chess.setName("象棋"); } else if (i == 2) { chess.setName("跳棋"); } else if (i == 3) { chess.setName("五子棋"); } try { chess.setDate(new SimpleDateFormat("yyyyMMdd").parse("2010061" + i)); } catch (ParseException e) { e.printStackTrace(); } chList.add(chess); } return chList; } }
※class上多個@RemoteProxy,其他方法上面都是@RemoteMethod
※vo
@DataTransferObject public class Chess{ @RemoteProperty private Long id; @RemoteProperty private String name; @RemoteProperty private Date date; // setter/getter... }
※java bean要轉換要使用@DataTransferObject,而欄位用@RemoteProperty
沒有留言:
張貼留言