(1)配置基础装备。每个符合Java EE规范的web应用程序都需要符合相应的目录结构,如图所示。

 


基于Myeclipse9.1的spring3.1MVC开发搭建_xml
 

 

 

工作之初,我们需要构建web应用的基础结构。不过,现在的IDE通常都有良好的Web开发支
持,MyEclipse9.1下载与安装请参见:http://guoyiqi.iteye.com/blog/1182653

剩下的只是按照Wizard的说明构建一个web应用的工程就行了。

    a)配置web.xml。我们需要将org.springframework.Web.servlet.DispatcherServlet和
org.springframework.Web.context.ContextLoaderListener通过<servlet>和<listener>元素添
加至web.xml部署描述符文件中。另外,出于其他目的考虑,我们还可以
添加相应的Filter以及ServletContextListener以处理字符编码和Log4j初始化等配置内容,这些
完全根据当前应用程序的需求情况来决定。现在,我们的web.xml看起来如代码清单所示。

 

Xml代码 基于Myeclipse9.1的spring3.1MVC开发搭建_sql_02 基于Myeclipse9.1的spring3.1MVC开发搭建_html_03基于Myeclipse9.1的spring3.1MVC开发搭建_xml_04
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.   
  7.   
  8.     <!-- Filter 定义  -->  
  9.     <!-- Character Encoding filter -->  
  10.     <filter>  
  11.         <filter-name>encodingFilter</filter-name>  
  12.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  13.         <init-param>  
  14.             <param-name>encoding</param-name>  
  15.             <param-value>UTF-8</param-value>  
  16.         </init-param>  
  17.         <init-param>  
  18.             <param-name>forceEncoding</param-name>  
  19.             <param-value>true</param-value>  
  20.         </init-param>  
  21.     </filter>  
  22.     <!-- Filter 映射 -->  
  23.     <filter-mapping>  
  24.         <filter-name>encodingFilter</filter-name>  
  25.         <url-pattern>/*</url-pattern>  
  26.     </filter-mapping>  
  27.   
  28.     <!--Spring的ApplicationContext 载入 -->  
  29.     <listener>  
  30.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  31.     </listener>  
  32.   
  33.     <!-- Spring 刷新Introspector防止内存泄露 -->  
  34.     <listener>  
  35.         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
  36.     </listener>  
  37.   
  38.   
  39.     <!--配置Sring MVC的核心控制器DispatcherServlet -->  
  40.   
  41.     <servlet>  
  42.         <servlet-name>dispatcherServlet</servlet-name>  
  43.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  44.     </servlet>  
  45.   
  46.     <!--为DispatcherServlet建立映射 -->  
  47.   
  48.     <servlet-mapping>  
  49.         <servlet-name>dispatcherServlet</servlet-name>  
  50.         <url-pattern>*.do</url-pattern>  
  51.     </servlet-mapping>  
  52.   
  53.   
  54.   
  55.   
  56.     <!-- session超时定义,单位为分钟 -->  
  57.     <session-config>  
  58.         <session-timeout>20</session-timeout>  
  59.     </session-config>  
  60.   
  61.     <!-- 出错页面定义 -->  
  62.     <error-page>  
  63.         <exception-type>java.lang.Throwable</exception-type>  
  64.         <location>/common/500.jsp</location>  
  65.     </error-page>  
  66.     <error-page>  
  67.         <error-code>500</error-code>  
  68.         <location>/common/500.jsp</location>  
  69.     </error-page>  
  70.     <error-page>  
  71.         <error-code>404</error-code>  
  72.         <location>/common/404.jsp</location>  
  73.     </error-page>  
  74.     <error-page>  
  75.         <error-code>403</error-code>  
  76.         <location>/common/403.jsp</location>  
  77.     </error-page>  
  78.   
  79.   
  80.   
  81.     <welcome-file-list>  
  82.         <welcome-file>index.jsp</welcome-file>  
  83.     </welcome-file-list>  
  84. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


	<!-- Filter 定义  -->
	<!-- Character Encoding filter -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<!-- Filter 映射 -->
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!--Spring的ApplicationContext 载入 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Spring 刷新Introspector防止内存泄露 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>


	<!--配置Sring MVC的核心控制器DispatcherServlet -->

	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>

	<!--为DispatcherServlet建立映射 -->

	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>




	<!-- session超时定义,单位为分钟 -->
	<session-config>
		<session-timeout>20</session-timeout>
	</session-config>

	<!-- 出错页面定义 -->
	<error-page>
		<exception-type>java.lang.Throwable</exception-type>
		<location>/common/500.jsp</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/common/500.jsp</location>
	</error-page>
	<error-page>
		<error-code>404</error-code>
		<location>/common/404.jsp</location>
	</error-page>
	<error-page>
		<error-code>403</error-code>
		<location>/common/403.jsp</location>
	</error-page>



	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

  b)WebApplicationContext文件的添加。我们需要在web-inf目录下添加org.springframework.Web.servlet.Dispatcherservlet和org.springframework.Web.context.ContextLoaderListener对应的WebApplicationContext配置文件(/WEB-INF/applicationContext.xml和
/WEB-INF/dispatcherServlet-servlet.xml)。这完全可以从Spring下载文件包中的sample目录下任何一个Web
应用程序中复制过来。只不过,需要根据命名规则修改相应的文件名称。当然,复制过来的文件内容
最好是清空,只保留可以在当前应用中能够通用的配置内容。

    以上基础设施构建完成之后,我们开始“盖楼”。

    (2)开发独立的业务逻辑。对于一个设计良好的web应用程序来说,虽然Web层依赖于业务层对象,
但业务层却不应该对Web层有任何的依赖。web层只应该看作是公开业务逻辑的一种视角或者交互方
式。这样做或者说这样看待系统的好处在于,业务层可以独立设计并实现,而不需要关心最终通过什
么手段将服务公开给用户。鉴于这样的理念,我们完全可以从业务层开始着手设计和实现。

entity - 领域模型层

    使用Sql First的开发模式,先设计数据库,参考DBA的性能意见而不要太片面追求OO化的表结构。

   

Java代码 基于Myeclipse9.1的spring3.1MVC开发搭建_sql_02 基于Myeclipse9.1的spring3.1MVC开发搭建_html_03基于Myeclipse9.1的spring3.1MVC开发搭建_xml_04
  1. package entity;   
  2.   
  3. public class Author {   
  4.     private String auId;   
  5.     private String auLname;   
  6.     private String phone;   
  7.        
  8.     public String getAuId() {   
  9.         return auId;   
  10.     }   
  11.     public void setAuId(String auId) {   
  12.         this.auId = auId;   
  13.     }   
  14.     public String getAuLname() {   
  15.         return auLname;   
  16.     }   
  17.     public void setAuLname(String auLname) {   
  18.         this.auLname = auLname;   
  19.     }   
  20.     public String getPhone() {   
  21.         return phone;   
  22.     }   
  23.     public void setPhone(String phone) {   
  24.         this.phone = phone;   
  25.     }   
  26.   
  27. }  
package entity;

public class Author {
	private String auId;
	private String auLname;
	private String phone;
	
	public String getAuId() {
		return auId;
	}
	public void setAuId(String auId) {
		this.auId = auId;
	}
	public String getAuLname() {
		return auLname;
	}
	public void setAuLname(String auLname) {
		this.auLname = auLname;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}

}

 

access - 资源访问层

    资源访问层包括对数据库、JMS、外部的WebService等的访问。

    每个领域对象对应一个DAO类。

Java代码 基于Myeclipse9.1的spring3.1MVC开发搭建_sql_02 基于Myeclipse9.1的spring3.1MVC开发搭建_html_03基于Myeclipse9.1的spring3.1MVC开发搭建_xml_04
  1. package dao;   
  2.   
  3. import java.sql.ResultSet;   
  4. import java.sql.SQLException;   
  5. import java.util.List;   
  6.   
  7. import org.springframework.jdbc.core.JdbcTemplate;   
  8. import org.springframework.jdbc.core.RowMapper;   
  9. import org.springframework.jdbc.core.RowMapperResultSetExtractor;   
  10.   
  11. import entity.Author;   
  12.   
  13.   
  14. public class AuthorDao {   
  15.     class UserRowMapper implements RowMapper{   
  16.         public Object mapRow(ResultSet rs,int index) throws SQLException        {   
  17.             Author author = new Author();   
  18.             author.setAuId(rs.getString(1));   
  19.             author.setAuLname(rs.getString(2));   
  20.             author.setPhone(rs.getString(3));   
  21.             return author;   
  22.         }   
  23.     }   
  24.   
  25.   
  26.     private JdbcTemplate jdbcTemplate;   
  27.   
  28.     public JdbcTemplate getJdbcTemplate() {   
  29.         return jdbcTemplate;   
  30.     }   
  31.   
  32.     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {   
  33.         this.jdbcTemplate = jdbcTemplate;   
  34.     }   
  35.   
  36.     public List select(String where) {   
  37.         List list;   
  38.   
  39.         String sql = "select * from authors";   
  40.   
  41.         list = jdbcTemplate.query(sql,new RowMapperResultSetExtractor(new UserRowMapper()));   
  42.   
  43.         return list;   
  44.   
  45.   
  46.     }   
  47.   
  48. }  
package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;

import entity.Author;


public class AuthorDao {
	class UserRowMapper implements RowMapper{
		public Object mapRow(ResultSet rs,int index) throws SQLException        {
            Author author = new Author();
            author.setAuId(rs.getString(1));
            author.setAuLname(rs.getString(2));
            author.setPhone(rs.getString(3));
            return author;
        }
    }


	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public List select(String where) {
		List list;

        String sql = "select * from authors";

        list = jdbcTemplate.query(sql,new RowMapperResultSetExtractor(new UserRowMapper()));

        return list;


	}

}

 

service - 业务逻辑层

    Service层有两类对象,

    一类是领域对象管理类(Entity Manager), 按领域对象划分,每个Manager类负责管理多个紧密关联的Entity的增删改查及其业务逻辑。

    一类是业务服务类(Service),按业务脚本划分,可能会访问到多种领域对象与Manager类。

Java代码 基于Myeclipse9.1的spring3.1MVC开发搭建_sql_02 基于Myeclipse9.1的spring3.1MVC开发搭建_html_03基于Myeclipse9.1的spring3.1MVC开发搭建_xml_04
  1. package service;   
  2.   
  3. import java.util.List;   
  4.   
  5. import dao.AuthorDao;   
  6. import entity.Author;   
  7.   
  8. public class AuthorService {   
  9.     private AuthorDao authorDao;   
  10.     public AuthorDao getAuthorDao() {   
  11.         return authorDao;   
  12.     }   
  13.     public void setAuthorDao(AuthorDao authorDao) {   
  14.         this.authorDao = authorDao;   
  15.     }   
  16.     public List<Author> getAuthorsList(){   
  17.         return authorDao.select("");   
  18.     }   
  19.   
  20. }  
package service;

import java.util.List;

import dao.AuthorDao;
import entity.Author;

public class AuthorService {
	private AuthorDao authorDao;
	public AuthorDao getAuthorDao() {
		return authorDao;
	}
	public void setAuthorDao(AuthorDao authorDao) {
		this.authorDao = authorDao;
	}
	public List<Author> getAuthorsList(){
		return authorDao.select("");
	}

}

 

web - Web MVC层

    MVC框架使用SpringMVC框架 ,每个Action实现一组页面操作。

Java代码 基于Myeclipse9.1的spring3.1MVC开发搭建_sql_02 基于Myeclipse9.1的spring3.1MVC开发搭建_html_03基于Myeclipse9.1的spring3.1MVC开发搭建_xml_04
  1. package web;   
  2.   
  3. import java.util.HashMap;   
  4. import java.util.List;   
  5. import java.util.Map;   
  6.   
  7. import javax.servlet.http.HttpServletRequest;   
  8. import javax.servlet.http.HttpServletResponse;   
  9.   
  10. import org.springframework.web.servlet.ModelAndView;   
  11. import org.springframework.web.servlet.mvc.Controller;   
  12.   
  13. import service.AuthorService;   
  14. import entity.Author;   
  15.   
  16. public class AuthorAction implements Controller {   
  17.        
  18.        
  19.   
  20.     private AuthorService authorService;   
  21.   
  22.     private String viewName; // 用于获取配置文件中的viewPage属性   
  23.   
  24.     public ModelAndView handleRequest(HttpServletRequest req,   
  25.             HttpServletResponse res) throws Exception {   
  26.         Map<String,List<Author>> model=new HashMap<String,List<Author>>();   
  27.         List<Author> authors=authorService.getAuthorsList();   
  28.         model.put("model", authors);   
  29.         return new ModelAndView(this.getViewName(), model); // 调用getViewPage获取要返回的页面   
  30.   
  31.     }   
  32.   
  33.     public String getViewName() {   
  34.         return viewName;   
  35.     }   
  36.   
  37.     public void setViewName(String viewName) {   
  38.         this.viewName = viewName;   
  39.     }   
  40.   
  41.     public AuthorService getAuthorService() {   
  42.         return authorService;   
  43.     }   
  44.   
  45.     public void setAuthorService(AuthorService authorService) {   
  46.         this.authorService = authorService;   
  47.     }   
  48.   
  49.   
  50. }  
package web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import service.AuthorService;
import entity.Author;

public class AuthorAction implements Controller {
	
	

	private AuthorService authorService;

	private String viewName; // 用于获取配置文件中的viewPage属性

	public ModelAndView handleRequest(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		Map<String,List<Author>> model=new HashMap<String,List<Author>>();
		List<Author> authors=authorService.getAuthorsList();
		model.put("model", authors);
		return new ModelAndView(this.getViewName(), model); // 调用getViewPage获取要返回的页面

	}

	public String getViewName() {
		return viewName;
	}

	public void setViewName(String viewName) {
		this.viewName = viewName;
	}

	public AuthorService getAuthorService() {
		return authorService;
	}

	public void setAuthorService(AuthorService authorService) {
		this.authorService = authorService;
	}


}

 

    View模板用JSP2.0 , 尽量使用纯html+JSP2.0 EL展示页面。

Java代码 基于Myeclipse9.1的spring3.1MVC开发搭建_sql_02 基于Myeclipse9.1的spring3.1MVC开发搭建_html_03基于Myeclipse9.1的spring3.1MVC开发搭建_xml_04
  1. <%@ page language="java" import="java.util.*,entity.*" pageEncoding="UTF-8"%>   
  2. <%@ include file="/common/taglibs.jsp" %>   
  3. <%   
  4. String path = request.getContextPath();   
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  6.   
  7. %>   
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  10. <html>   
  11.   <head>   
  12.     <base href="<%=basePath%>">   
  13.        
  14.     <title>My JSP 'authorsList.jsp' starting page</title>   
  15.        
  16.     <meta http-equiv="pragma" content="no-cache">   
  17.     <meta http-equiv="cache-control" content="no-cache">   
  18.     <meta http-equiv="expires" content="0">       
  19.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  20.     <meta http-equiv="description" content="This is my page">   
  21.     <!--   
  22.     <link rel="stylesheet" type="text/css" href="styles.css">   
  23.     -->   
  24.   
  25.   </head>   
  26.      
  27.   <body>   
  28.     This is my JSP page2. <br>   
  29.     
  30.      <c:forEach items="${model}" var="author">   
  31.      ${author.auId},${author.auLname }${author.phone }<br/>   
  32.         
  33.      </c:forEach>   
  34.   </body>   
  35. </html>  
<%@ page language="java" import="java.util.*,entity.*" pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'authorsList.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    This is my JSP page2. <br>
 
     <c:forEach items="${model}" var="author">
     ${author.auId},${author.auLname }${author.phone }<br/>
     
     </c:forEach>
  </body>
</html>

 

    Javascript与Ajax使用JQuery或Dojo Base。

    尽量采用CSS框架规范CSS的布局。

 

(3)配置

applicationContext.xml

Xml代码 基于Myeclipse9.1的spring3.1MVC开发搭建_sql_02 基于Myeclipse9.1的spring3.1MVC开发搭建_html_03基于Myeclipse9.1的spring3.1MVC开发搭建_xml_04
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">  
  5.   
  6.   
  7.     <!-- 定义受环境影响易变的变量 -->  
  8.     <bean  
  9.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  10.         <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />  
  11.         <property name="ignoreResourceNotFound" value="true" />  
  12.         <property name="locations">  
  13.             <list>  
  14.                 <!-- 标准配置 -->  
  15.                 <value>/WEB-INF/application.properties</value>  
  16.             </list>  
  17.         </property>  
  18.     </bean>  
  19.   
  20.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  21.         destroy-method="close">  
  22.         <!-- Connection Info -->  
  23.         <property name="driverClassName" value="${jdbc.driver}" />  
  24.         <property name="url" value="${jdbc.url}" />  
  25.         <property name="username" value="${jdbc.username}" />  
  26.         <property name="password" value="${jdbc.password}"></property>  
  27.   
  28.         <!-- Connection Pooling Info -->  
  29.         <property name="maxIdle" value="${dbcp.maxIdle}" />  
  30.         <property name="maxActive" value="${dbcp.maxActive}" />  
  31.         <property name="defaultAutoCommit" value="false" />  
  32.         <property name="timeBetweenEvictionRunsMillis" value="3600000" />  
  33.         <property name="minEvictableIdleTimeMillis" value="3600000" />  
  34.     </bean>  
  35.   
  36.     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  37.         <property name="dataSource">  
  38.             <ref bean="dataSource" />  
  39.         </property>  
  40.     </bean>  
  41.     <bean id="authorDao" class="dao.AuthorDao">  
  42.         <property name="jdbcTemplate">  
  43.             <ref bean="jdbcTemplate" />  
  44.         </property>  
  45.     </bean>  
  46.     <bean id="authorService" class="service.AuthorService">  
  47.         <property name="authorDao">  
  48.             <ref bean="authorDao" />  
  49.         </property>  
  50.     </bean>  
  51. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


	<!-- 定义受环境影响易变的变量 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<!-- 标准配置 -->
				<value>/WEB-INF/application.properties</value>
			</list>
		</property>
	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<!-- Connection Info -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}"></property>

		<!-- Connection Pooling Info -->
		<property name="maxIdle" value="${dbcp.maxIdle}" />
		<property name="maxActive" value="${dbcp.maxActive}" />
		<property name="defaultAutoCommit" value="false" />
		<property name="timeBetweenEvictionRunsMillis" value="3600000" />
		<property name="minEvictableIdleTimeMillis" value="3600000" />
	</bean>

	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>
	<bean id="authorDao" class="dao.AuthorDao">
		<property name="jdbcTemplate">
			<ref bean="jdbcTemplate" />
		</property>
	</bean>
	<bean id="authorService" class="service.AuthorService">
		<property name="authorDao">
			<ref bean="authorDao" />
		</property>
	</bean>
</beans>

 

dispatcherServlet-servlet.xml

Xml代码 基于Myeclipse9.1的spring3.1MVC开发搭建_sql_02 基于Myeclipse9.1的spring3.1MVC开发搭建_html_03基于Myeclipse9.1的spring3.1MVC开发搭建_xml_04
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.   
  5.     <bean id="localeResolver"  
  6.         class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">  
  7.     </bean>  
  8.   
  9.     <!--配置控制器的映射-->  
  10.   
  11.     <bean id="urlMapping"  
  12.         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  13.         <property name="mappings">  
  14.             <props>  
  15.                 <prop key="authorAction.do">authorAction</prop>  
  16.             </props>  
  17.         </property>  
  18.     </bean>  
  19.   
  20.     <!--配置视图-->  
  21.   
  22.     <bean id="viewResolver"  
  23.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  24.         <property name="prefix" value="/WEB-INF/content/" />  
  25.         <property name="suffix" value=".jsp" />  
  26.   
  27.     </bean>  
  28.   
  29.     <!--指定控制器的实现类,并且配置其参数的值-->  
  30.   
  31.     <bean id="authorAction" class="web.AuthorAction">  
  32.         <property name="viewName">  
  33.             <value>authorsList</value>  
  34.         </property>  
  35.         <property name="authorService">  
  36.             <ref bean="authorService" />  
  37.         </property>  
  38.     </bean>  
  39.   
  40. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<bean id="localeResolver"
		class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
	</bean>

	<!--配置控制器的映射-->

	<bean id="urlMapping"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="authorAction.do">authorAction</prop>
			</props>
		</property>
	</bean>

	<!--配置视图-->

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/content/" />
        <property name="suffix" value=".jsp" />

	</bean>

	<!--指定控制器的实现类,并且配置其参数的值-->

	<bean id="authorAction" class="web.AuthorAction">
		<property name="viewName">
			<value>authorsList</value>
		</property>
		<property name="authorService">
			<ref bean="authorService" />
		</property>
	</bean>

</beans>

 

(4)发布运行