springmvc -- springMVC笔记1--HelloWorld
环境配置:
创建项目SpringMVC1
导入jar包,添加web.xml和springMVC-servle.xml到WEB-INF下
web.xml
- springMVC-servlet.xml
<beans xmlns=”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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/content/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
- 创建controller层
package com.firefly.springmvc1.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloWorld implements Controller{
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
System.out.println("-----hello mvc-----");
ModelAndView mv = new ModelAndView();
mv.addObject("msg","hello springmvc");
mv.setViewName("welcome");
return mv;
}
}
创建welcome.jsp文件
运行测试:输入welcome
可能遇到的问题
springMVC的xml文件报错,有可能是与以前的spring的xml文件版本的不同,eclipse自动保存了xml版本的信息,清理方法:window->preferences->general->network connections->cache,将里面所有的东西都移除掉
启动tomcat时找不到springMVC的xml,检查xml的名字
