欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring MVC 初使用

程序员文章站 2022-06-22 09:26:46
环境: myeclipse+tomcat8 1.在WebRoot下新建:web.xml: _________________________________________________________________________________________________________ ......

环境:

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 的初次使用

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