Spring Tool Suite生成默认的MVC项目的配置文件问题
1、STS是开发Spring程序的首选,基于JavaEE的程序,我都用STS来开发,但是在生成默认的MVC项目时,其配置文件让人很讨厌,在许多选项上都会加一个beans,如<beans:beans>,<beans:bean>,<beans:property>,但是看标准的文档,都是用<beans>、<bean>之类的标签,很纳闷,甚至觉得是xsd版本的问题,后来观察了一个netbeans生成的spring项目配置文件,才恍然大悟。
2、默认生成的配置文件如下
<?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.shiyq.study" />
</beans:beans>
可以看出默认的命名空间为 xmlns="http://www.springframework.org/schema/mvc",这个就是常见的mvc空间,看下面的<annotation-driven/>和<resources>没有加mvc标签,就知道了,命名空间beans对应的是xmlns:beans="http://www.springframework.org/schema/beans",到这里就可以明白为什么会出现<beans:beans>模式的标签了,根标签为beans,还增加一个名称空间,所以解决办法也很简单,将默认的命名空间设置为beans对应的空间,默认的命名空间命名为"mvc",然后修改相应的标签,大致如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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 -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.shiyq.study" />
</beans>
问题解决起来很简单,不过其实不改也没有太大影响,就是多写几个字母而已,但看起来清爽了不少,也算了解了一点xml的配置知识,所以随笔记下。