首先创建一个web工程
配置web.xml文件
在src路径下创建springmvc.xml文件
然后配置springmvc.xml文件(Controller都是依据注解开发的)
<!-- 配置视图解析器 -->
<!-- jsp视图解析器,支持jstl -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图地址前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 视图地址后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置扫描的Controller -->
<context:component-scan base-package="com.spring.mvc.test"></context:component-scan>
<!-- 注解类型的处理器适配器和处理器映射器 -->
<mvc:annotation-driven></mvc:annotation-driven>
接着就是Controller的代码
@Controller
public class ControllerSpring {
@RequestMapping("/helloworld")
public ModelAndView hello(){
ModelAndView model = new ModelAndView();
model.addObject("message", "<h1>HelloWorld</h1>");
model.setViewName("Hello");
return model;
}
}
接着写Jsp文件根据视图解析器的路径来配置和编写
启动Tomcat服务器在浏览器的地址栏输入:http://localhost:8080/Springmvc01/helloworld
就可以输出HelloWorld