Spring MVC 配置

fangyuylc 2014-03-25

SpringMVC模式:

SpringMVC模式下,把其它的一些组件当作一个个的bean对象来处理,Spring充当一个对象工厂的作用。

SpringMVC搭建方法-->

1:导入Spring开发的Jar和相关的依赖包

2:在web.xml文件中,配置Servlet信息

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

<servlet>

<servlet-name>SSHOver</servlet-name>

<servlet-class>

org.springframework.web.servlet.DispatcherServlet

</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>SSHOver</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<display-name></display-name>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

3:编写控制器测试文件

在src目录下新建一个控制器文件

4:在WEB-INF目录下编写一个Spring.xml文件,用来处理将请求分派出去,映射相关的Action请求。

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"

xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:task="http://www.springframework.org/schema/task"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-3.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<beanclass="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<propertyname="mappings">

<props>

<propkey="start.do">startController</prop>

</props>

</property>

</bean>

<beanid="startController"class="com.test.StartController"></bean>

</beans>

5:编译测试运行项目。

相关推荐