设计流程

.do请求----DispactcherServlet----handlerMapping—NewsListController-ViewResolver-news.jsp

web.xml配置DispacherServlet

 <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>
  	org.springframework.web.servlet.DispatcherServlet
  	</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:spring-mvc.xml,classpath:applicationContext.xml</param-value>
  	</init-param>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

spring-mvc.xml配置handlerMapping

  <bean id="handlerMapper" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  		<property name="mappings">
				<props>
				<!--请求为hello.do调用id=helloController-->
				       <prop key="/hello.do">helloController</prop>
				       <prop key="/news.do">newsListController</prop>
				</props>
      </property>
  </bean>

编写controller组件

public class NewsListController implements Controller{

	//获得NewsDao对象,进行setter注入
	private FanNewsDao fanNewsDao;
	
	public void setFanNewsDao(FanNewsDao fanNewsDao) {
		this.fanNewsDao = fanNewsDao;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
				
		//  返回数据设置
		ModelAndView mav=new ModelAndView();
		//设置返回的jsp
		mav.setViewName("news");
	List<FanNews> list=fanNewsDao.loadAll();
		mav.getModel().put("news", list);
		return mav;
	}

}

FanNewDao接口实现类,通过注解标志创建对象,fanNewsDao就是Controller配置的属性ref引用的id

//组件扫描,默认id将类名首字母小写构建对象

@Repository("fanNewsDao")
public class JdbcTemplateFanNews implements FanNewsDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
		public List<FanNews> loadAll() {
	
		 String sql="select *from fan_news";
		 
		  List<FanNews> list=jdbcTemplate.query(sql,new BeanPropertyRowMapper<FanNews>(FanNews.class));
		  
		  for(FanNews news:list) {
		  System.out.println(news.getFan_name()+news.getFan_item()); 
		  }
		return list;
	}

}

注释TODO,//TODO window–其他 —tasks,双击定位注解
添加请求追加定义

spring-mvc.xml配置controller

//自动调用set方法,注入fanNewsDao对象
将id为fanNewsDao注入,

   <!--   配置newsListController -->
    <bean id="newsListController" class="com.tracy.controller.NewsListController">
    	<property name="fanNewsDao" ref="fanNewsDao"></property>
    </bean>

配置文件,在web.xml初始化配置,加载两个xml文件,将两个xml配置到一个容器,spring-mvc.xml/applicationContext.xml
下面是applicationContext.xml配置

<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="username" value="root"></property>
	<property name="password" value="123456"></property>
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<constructor-arg index="0"  ref="dbcp"></constructor-arg>
</bean>

<!-- 开启组件扫描  @Contorller @Service @Repository @Component@Resource @Autowired-->
 <context:component-scan base-package="com.tracy.dao.impl" />
<!--  <context:component-scan base-package="*"></context:component-scan> -->
  </beans>

配置viewResolver

    <!--   配置ViewResolver配置spring-mvc.xml通用资源适配解析器前缀  后缀 -->
<bean id="ModelAndView" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property  name="prefix" value="/"></property>
          <property  name="suffix" value=".jsp"></property>
</bean>

news.jsp页面显示

使用EL表达式 JSTL标签,引入标签库

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>资讯列表</h1>
${news}
<table>
<c:forEach items="${news}" var="n">
	<tr>
		<td>${n.id} </td>
		<td>${n.fan_item} </td>
	</tr>

</c:forEach>
</table>
</body>
</html>

在这里插入图片描述

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐