搭建spring web项目--spring2.5和spring mvc

简单说明:

spring2.5+spring mvc+jsp的应用,web服务器为tomcat6.0

 

1、eclipse里新建web项目

      new-->other-->web-->Dynamic Web Project

 

2、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>wsd</display-name>
  
  <!-- Log4j 指定 Log4J 配置文件的地址 -->
  <context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/log4j.properties</param-value>
	</context-param>
	
	<!--  -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/application-context.xml</param-value>
	</context-param>
	
	<!-- 中文乱码过滤器 -->
	<filter><!-- Spring 编辑过滤器 -->
		<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-mapping><!--  过滤器的匹配 URL -->
		<filter-name>encodingFilter</filter-name>
		<servlet-name>action</servlet-name>
	</filter-mapping>
	
	<!-- 使用该监听器初始化 Log4J 日志引擎 -->
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- Introspector 缓存清除监听器 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	
	<!-- spring mvc  -->
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>namespace</param-name>
			<param-value>application-action</param-value>
		</init-param>
		<!-- 
		web容器(tomcat)启动的时候就加载这个Servlet,以便初始化该Servlet。
   		当该值>=0的时候,web容器启动就加载,并且该值越小越优先  
    	-->
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

3、创建application-context.xml并配置

<?xml version="1.0" encoding="UTF-8"?>
<!-- 命名空间,使用注解注入bean 需要 xmlns:context 和它底下的schema文件 -->
<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"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	<!-- 属性占位符,多个文件用逗号隔开 -->
	<context:property-placeholder location="/WEB-INF/jdbc.properties" />
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
		p:driverClassName="${jdbc.driverClassName}"	p:url="${jdbc.url}" p:username="${jdbc.username}" 
		p:password="${jdbc.password}"  p:initialSize="1" p:maxActive="500" p:maxIdle="2" p:minIdle="1" />
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 
		p:dataSource-ref="dataSource" />
		
	<tx:annotation-driven />
	<!-- 
	隐式注册了多个对注释进行解析处理的处理器 AutowiredAnnotationBeanPostProcessor,
	CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor, RequireAnnotationBeanPostProcessor
	所用jar包为lib\common-annotations.jar
	-->
	<context:annotation-config />
	<!-- 
	自动扫描的包 ,扫描 注释了@Component,@Service,@Controller,@Repository 
	@Service用于注释业务层组件、@Controller用于标注控制层组件、
	@Repository用于数据访问组件即DAO组件、@Component泛指组件
	-->
	<context:component-scan base-package="self.lx.application.internal" />
</beans>

 

4、创建application-action.xml并配置

<?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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
	<context:component-scan base-package="self.lx.application.web.action" />
	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
	<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"	p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>

 

5、log4j配置:log4j.properties

#配置log4j的根logger "og4j.rootLogger = [ level ] , appenderName"
log4j.rootLogger=INFO, stdout
#输出到控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
 

6、application的文件目录结构


 说明:src下各包包括:DAO层、业务层、控制层(负责页面跳转)

 

7、DAO层 实做部分代码

@Repository
public class JdbcUserDao implements IUserDao{

	private DataSource dataSource;
	private SimpleJdbcTemplate template;
	
	@Autowired
	public JdbcUserDao(DataSource dataSource){
		this.dataSource= dataSource;
		template = new SimpleJdbcTemplate(this.dataSource);
	}
	
	@Override
	public List<User> getAllUser() {
		return template.query("select * from users order by id desc", ParameterizedBeanPropertyRowMapper.newInstance(User.class));
	}

}

 

8、业务层实做部分代码

@Service
public class UserManager implements IUserManager {

	private IUserDao userDao;

	@Autowired
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	
	public List<User> getAllUser() {
		return userDao.getAllUser();
	}
}

 

9、控制层部分代码

@Controller
public class UserAction {
	
	private IUserManager userManager;

	@Autowired
	public void setUserManager(IUserManager userManager) {
		this.userManager = userManager;
	}
	
	@RequestMapping("/user/list.do")
	public String list(Model model) {
		model.addAttribute("results", userManager.getAllUser());
		return "user/listUser";
	}

}

 

10、前台显示页面listUser.jsp

<table>
<tr>
	<td>用户名</td>
	<td>密码</td>
</tr>
<c:forEach var="user" items="${results}" varStatus="s">
<tr>
	<td><c:out value="${user.username}" /></td>
	<td><c:out value="${user.password}" /></td>
</tr>
</c:forEach>
</table>

你可能感兴趣的:(spring,Web,mvc,log4j,servlet)