----web.xml配置----
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,
名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value> 默认
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
----springMVC-servlet.xml配置----(src下)
spring-servlet这个名字是因为上面web.xml中<servlet-name>标签配的值为springMVC(<servlet-name>springMVC</servlet-name>),再加上“-servlet”后缀而形成的springMVC-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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
< A href= "http://www.springframework.org/schema/context/spring-context-3.0.xsd">
http://www.springframework.org/schema/context/spring-context-3.0.xsd
</ A>"
>
<!-- servlet在找不到的时候会去找静态的内容,如jpg,js,css -->
<
mvc:default-servlet-handler
/>
<!-- 启用spring mvc 注解 -->
<
mvc:annotation-driven
/>
< context:component-scan base-package= "controller"></ context:component-scan>
<!-- 完成请求和注解POJO的映射 -->
< bean class= "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
<
bean
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
id
=
"internalResourceViewResolver"
>
<!-- 前缀 -->
<
property
name
=
"prefix"
value
=
"/WEB-INF/jsp/"
/>
<!-- 后缀 -->
<
property
name
=
"suffix"
value
=
".jsp"
/>
</
bean
>
----使用----
http://localhost:8080/项目名/mvc/hello
@Controller
@RequestMapping
(
"/mvc"
)
public
class
mvcController {
@RequestMapping
(
"/hello"
)
@RequestMapping
public
String hello(String
name
return
"hello"
;
}
}