Spring3.2.2开发与整合+整合JSTL

一个新项目是用spring来开发的,我也是第一次接触,但是感觉非常强大。。。

在导入包的时候,我发现网上没有一个比较好的指导最新版本spring最少应该导入哪些包,特此书写本文,以来给那些需要的人以帮助。同时构建一个简单的测试输出!

仅是个人见解,大神勿喷。

1.我采用的是spring-framework-3.2.2.RELEASE,是此时最新版本

2.官方下载后有58个包,其中javadoc为19个,sources为19个,剩下的都是可能用到的lib

3.搭建一个最简单的spring程序(不涉及到数据库等,只是最简单的应用输出,同时整合了JSTL)

    1)导入所需要的包

     spring-beans-3.2.2.RELEASE.jar
     spring-context-3.2.2.RELEASE.jar
     spring-core-3.2.2.RELEASE.jar
     spring-expression-3.2.2.RELEASE.jar
     spring-web-3.2.2.RELEASE.jar
     spring-webmvc-3.2.2.RELEASE.jar 

     commons-logging-1.1.2.jar
     jstl-1.2.jar

  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_3_0.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
	<display-name>mySpring2</display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
				classpath:com/spring/demo/xml/context-*.xml
		</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
				/WEB-INF/config/springmvc/dispatcher-servlet.xml
			</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

     3)配置dispatcher-servlet.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">
    <context:component-scan base-package="com.spring.demo"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
    p:viewClass="org.springframework.web.servlet.view.JstlView" 
    p:prefix="/WEB-INF/jsp/" 
    p:suffix=".jsp"/>
</beans>

    4)配置context-common.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">
	<context:component-scan base-package="com.spring.demo">
       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    
</beans>

    5)编写action

   

package com.spring.demo.action.common;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
	
	@RequestMapping(value="/common/test/testPrint.do")
	public String testPrint(ModelMap model){
		model.addAttribute("bl", "我是变量输出!");
		System.out.println("你存在我深深的脑海里");
		return "/common/test/testPrint";
	}
}

   6)输出模板

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
测试输出
<br>
${bl}
</body>
</html>

 好了,试试把

   

你可能感兴趣的:(Spring3)