您的位置:首页 > 编程语言 > Java开发

学习Spring Framework中遇到的各种问题

2014-03-10 18:37 405 查看
1、Maven Dependencies 与 Referenced Libraries有区别,程序中添加的第三方库文件.jar需要添加到Maven Dependencies对应的文件夹下面,也就是对应工程的\WEB-INF\lib路径下面,不然会出现错误ClassNotFoundException,添加方法是在pom.xml文件中添加dependency,dependency中的<groupId><artifactId><version>可以通过网址:http://mvnrepository.com/来查找,例如

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.1</version>
</dependency>


2、访问静态资源

首先需要在servlet-context.xml中进行配置
<resources mapping="/resources/**" location="/resources/" />
整个配置文件的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.ticket.controller" />

</beans:beans>


然后在对应的jsp中指定路径,此处我用的是绝对路径,相对路径的方法可在google中搜索jsp src找相关资料
<script type="text/javascript" src="<%=request.getContextPath()%>/resources/processing-1.4.1.js"> </script>


3、用HttpClient执行Get请求时,如果url中含有中文,需要进行URLEncoding处理,例如:
String city = "武汉";
String city_UTF8 = null;

/*如果url中含有中文参数,需要进行URLEncoding处理*/
try {
city_UTF8 = URLEncoder.encode(city,"UTF-8");
} catch (UnsupportedEncodingException e) {
System.out.println(city_UTF8);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: