MVC模式分析讲解与Type safety: Unchecked cast from Object to ArrayList<Reply>相关编译错误处理...

     总结一下今天写的代码,总觉得有一些需要拿来总结的,这些天一直困扰我的的MVC模式终于被我攻克了,自己写了一个比较容易理解的测试代码、

#################################################################

这是模型层

public static Collection<Team> allTeams() throws Exception {
		
		ArrayList<Team> teams = new ArrayList<Team>();//创建集合对象
		
		//从数据库获取数据
		Connection cn = DataSource.getConnection();
		Statement stmt = cn.createStatement();
		ResultSet rs = stmt.executeQuery("select * from team");
		
		while(rs.next()) {
			Team team = new Team();//创建team对象
			//用数据库取得的数据设置team对象的属性
			team.setId(rs.getInt("id"));
			team.setName(rs.getString("name"));
			team.setSlogan(rs.getString("slogan"));
			team.setLeader(rs.getString("leader"));
			
			teams.add(team);//把team对象放到集合teams中
		}

		return teams;
	}

 

 

这是控制层

protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			Collection<Team> teams = TeamBusiness.allTeams();

			request.setAttribute("teams", teams);

			RequestDispatcher rd = request
					.getRequestDispatcher("../viewTeams.jsp");
			rd.forward(request, response);

		} catch (Exception e) {
			e.printStackTrace();

			RequestDispatcher rd = request
					.getRequestDispatcher("../viewTeamsFail.jsp");
			rd.forward(request, response);
		}
	}

 

这是视图层

<table border="1" align="center">
<tr><th>组名</th><th>口号</th><th>组长</th></tr>
<%
Collection<Team> teams = (Collection<Team>)request.getAttribute("teams");//这个地方对报警告
Iterator<Team> it = teams.iterator();
while(it.hasNext()) {
	Team team = it.next();
%>
<tr>
<td><%=team.getName()%></td>
<td><%=team.getSlogan()%></td>
<td><%=team.getLeader()%></td>
</tr>
<%
}
%>
</table>

 

学过一点MVC模式的人都基本上能够很容易的看懂这几段代码,我们通过链接的访问servlet,servlet会通过doget的方式获取信息。然后定向到所要显示的页面。自己画了一张图容易理解。

 

 

 



  

最后想要解决相关- Type safety: Unchecked cast from Object to   ArrayList<Reply>的问题,自己做的项目报错,还以为是这个原因,最后证明与这个毫无关系,这只是简单的警告而已,意思是当Object类型转换为ArrayList类型会有危险。至于什么危险我也不知道。一下提供几点解决方案,只是让自己写的代码少一些警告而已,自己感觉没那个必要

第一:添加@SuppressWarnings("unchecked")  

第二:myeclipse的Window->Preferences->Java->Compiler->Errors/Warning->Generic types中Unchecked generic type operation设置为Ignore。

第三:myeclipse的Window->Preferences->Java->Compiler将Compiler compliance level 设置为小于1.5。

 

为什么javaeye没有微软雅黑这个字体,

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值