- 新建普通Java空项目
- 创建完成后右击项目 Add FrameWork Support...
- 勾选Web Application 和springMvc 再点击完成,回到主界面就可以看到web目录了
- 修改 默认的 web.xml
<display-name>springMVC</display-name> <!-- 部署 DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 表示容器再启动时立即加载servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 处理所有URL --> <url-pattern>/</url-pattern> </servlet-mapping>
- 在src下创建Controller类
package controller; 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 LoginController implements Controller { public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { return new ModelAndView("/WEB-INF/jsp/register.jsp"); } }
- 创建并修改
springmvc-servlet.xml
配置controller映射信息<!-- LoginController控制器类,映射到"/login" --> <bean name="/login" class="controller.LoginController"/> <!-- LoginController控制器类,映射到"/register" --> <bean name="/register" class="controller.RegisterController"/>
- 配置tomcat运行项目