一、新建一个项目,与SpringMVC入门相同
二、配置web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
三、配置springmvc配置文件
<!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理-->
<context:component-scan base-package="com.wy.controller"></context:component-scan>
<!--让SpringMVC不处理静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--支持注解驱动-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
四、新建hello.jsp
五、创建Controller
@Controller //代替了implements Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/h1")
public String hello(Model model){
//封装数据
model.addAttribute("msg","hello,SpringMVCAnnotation!!");
return "hello"; //会被视图解析器处理 WEB-INF/jsp/hello.jsp
}
}
六、运行tomcat
访问 localhost:8080/hello/h1