标签:xml web spring springframework mvc 注解 import servlet
1.1. 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns: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"
id="WebApp_ID" version="3.0">
<display-name>AnnoationSpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置spring mvc 的核心servlet -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 指定spring mvc 配置文件的路径 默认为 /WEB-INF/<[servlet-name]-servlet.xml> 例如此处为/WEB-INF/spring-servlet.xml
下面配置的也就是默认值 -->
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<!-- 配置随服务器启动自动初始化 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
|
1.2. 配置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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 指定注解扫描包 扫描使用了注解的类 Spring在启动的时候,会默认扫描自动扫描包下的所有的类,
为每个注解分配一个mapping。 -->
<context:component-scan base-package="com.morris.controller"></context:component-scan>
<!-- AnnotationMethodHandlerAdapter 是方法映射的,不同方法有不同url请求,在类找方法。
2)DefaultAnnotationHandlerMapping
DefaultAnnotationHandlerMapping根据扫描的包下面找类,即通过url找类 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<!-- 视图解析器 当返回逻辑视图时,InternalResourceViewResolver会加上前缀和后缀 -->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"></property>
<property name="prefix" value="/"></property>
</bean>
</beans>
|
1.3. 配置控制器
package com.morris.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping(value="/hello")
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("hello....");
return new ModelAndView("hello");
}
}
|
1.4. 测试
在浏览器地址栏输入
http://localhost:8080/annoationDemo/hello
标签:xml,
web,
spring,
springframework,
mvc,
注解,
import,
servlet
From: https://blog.51cto.com/u_6784072/6216872