spring MVC 初使用

大城市里の小女人 提交于 2020-03-29 12:28:00

环境:

myeclipse+tomcat8

1.在WebRoot下新建: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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>springMVC</display-name>
<!-- 部署 DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>   //servlet名字:springmvc
<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>


<!-- 表示容器再启动时立即加载servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 处理所有URL -->
<url-pattern>/</url-pattern>         url访问根目录映射到WebRoot下的index.jsp
</servlet-mapping>
</web-app>

2.在WebRoot下的index.jsp进行编辑:

_______________________________________________________________________________________________________________________________

<body>
This is my JSP page. <br>
<a href="${pageContext.request.contextPath}/register}">register</a>    //url访问根目录/register     映射到具体的jsp文件   //所以这是两个方面的处理(http与具体页面文件),要进行url映射,具体实现要在具体的servlet-servlet.xml文件进行映射了
<a href="${pageContext.request.contextPath}/login}">login</a>

</body>

3.接着上文,Controller控制器对应着具体的jsp页面,下面进行编辑:

______________________________________________________________________________________________________________________________

在src下新建controller 包,在controller中新建LoginController与RegisterController文件:

具体如下:

registercontroller:
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 RegisterController implements Controller {
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
return new ModelAndView("/WEB-INF/jsp/login.jsp");
}
}

LoginController:

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");
}

4.接下来进行Url 与具体页面的映射了:

_______________________________________________________________________________________________________________________________

在Web-INF下新建:

springmvc-servlet.xml:        //springmvc在web.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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.xsd">
<!-- LoginController控制器类,映射到"/login" -->
<bean name="/login" class="controller.LoginController"/>      //bean中的/login对应着LoginController,即Url访问根目录/login 对应着 LoginController的具体页面
<!-- LoginController控制器类,映射到"/register" -->
<bean name="/register" class="controller.RegisterController"/>
</beans>

___________________________________________________________________________________________________________________________

5.发布然后运行:

url访问http://localhost:8080/springmvcdemo01/

应该出现index.jsp页面:

然后点击链接进行页面的跳转了

以上为spring MVC 的初次使用

大神看了勿喷!小弟感激不尽啊!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!