spring MVC 初使用

2020-03-29 16:01:12来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

spring MVC 初使用

环境:

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

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

 _____________________________________________________________________________________________________

6.出错了,有个文件出错了:

Index.jsp:

<a href="${pageContext.request.contextPath}/login}">login</a>

改为:

<a href="${pageContext.request.contextPath}/login">login</a>

这样可能就好了,但还是出问题,我再找找吧     

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   


原文链接:https://www.cnblogs.com/broadencoder/p/12591467.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:15.面向对象-----this、package、import关键字

下一篇:Java连载104-线程的生命周期、命名与获取