java web项目中引入spring

自己动手实践了一次,发生中间出了一下问题,现整理出来,供参考。

Step1: 新建一个java web项目

Step2:下载spring的jar包http://repo.spring.io/libs-release-local/org/springframework/spring/
版本取决于你,我选择的是4.0.4的,下载好了之后进行解压,将libs文件夹中jar文件都添加项目的lib中。

注意其中commons-logging-1.1.1.jar包是需要另外下去添加进去的。

Step3: 配置spring功能。
首先在WEB-INF文件夹下创建一个xml文件:spring-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"  
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="  
    http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
    >  
  
      <!-- 解决中文乱码 begin -->  
      <bean id="utf8Charset" class="java.nio.charset.Charset"
          factory-method="forName">
        <constructor-arg value="UTF-8"/>
    </bean>
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg ref="utf8Charset"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>  
    <!-- 解决中文乱码 end -->  

    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    <!-- 处理静态资源begin --> 
    <mvc:default-servlet-handler />
    <!-- 处理静态资源end --> 
    <context:component-scan base-package="com.xsh.learnspring" /> 
</beans>  

Step4:配置web.xml,在该文件中加入以下内容:

 <!-- Spring Configures start -->  
	<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>
<!-- encoding filter for jsp page -->
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>  
    <servlet-name>spring</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
    <servlet-name>spring</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>  
<!-- Spring Configures end -->  

Step5:创建jspcontroller

<body>
    <input type = "button" value="test" onclick= "test()" id="test"/>
</body>
<script>
  	function test(){
  	var data = "请求";
		  $.ajax({
			  url: "test.do",
			  type:'post',
			  dataType: 'text',
			  data:{data:data},
			  success: function(datas){
			  console.log(datas);
			  }
		});
}
</script>

/** 
 * ClassName: SpringTestController <br/> 
 * Function: TODO ADD FUNCTION. <br/> 
 * Reason: TODO ADD REASON(可选). <br/> 
 * date: 2016-6-18 上午10:40:38 <br/> 
 * 
 * @author xieshuang 
 * @version  
 * @since JDK 1.6 
 */
@Controller
public class SpringTestController {

@RequestMapping ( "/test.do" )  
@ResponseBody
public String myTest(HttpServletRequest request,  
        HttpServletResponse response) throws Exception{  
	String data = request.getParameter("data");
	System.out.println("获取的参数为:" + data);
	String message = "访问成功";
    return message ;


}  
}

1、前后台交互传递数据时会出现中文乱码,其配置必须放在<context:component-scan base-package="com.xsh.learnspring" /> 上面

2、对项目静态资源的处理,例如引入的jquery.js
<mvc:default-servlet-handler />

posted @ 2016-06-18 20:52  河岸飞流  阅读(2354)  评论(0编辑  收藏  举报

那片笑声让我想起我的那些花儿
在我生命每个角落静静为我开着
我曾以为我会永远守在她身旁
今天我们已经离去在人海茫茫
她们都老了吧 她们在哪里呀
幸运的是我曾陪她们开放