Use early spring MVC

surroundings:

myeclipse+tomcat8

1. In WebRoot New: 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>


<! - represents a servlet loaded container immediately restart ->
<Load-ON-Startup>. 1 </ Load-ON-Startup>
</ servlet>
<servlet-Mapping>
<servlet-name> SpringMVC </ servlet- name>
<-! handle all the URL ->
<URL-pattern> / </ URL-pattern> URL mapping to access the root directory index.jsp under the WebRoot
</-mapping the servlet>
</ Web-App>

2. Edit the index.jsp under WebRoot:

_______________________________________________________________________________________________________________________________

<body>
This IS My JSP Page. <br>
<a href="${pageContext.request.contextPath}/register}"> the Register </a> // url to access the root directory / register mapped to specific files jsp / / so this is a process (http specific page file) two aspects to be mapped url, embodied to be mapped in a specific file servlet-servlet.xml
<a href = "$ {pageContext.request.contextPath } / Login} "> Login </a>

</ body>

3. Further to the above, the Controller controller jsp pages corresponding to a particular, for editing the following:

______________________________________________________________________________________________________________________________

Under the new controller in the src package, the new LoginController with RegisterController file in the controller:

details as follows:

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. Next Url mapped to specific pages on:

_______________________________________________________________________________________________________________________________

In the Web-INF New:

springmvc-servlet.xml: // springmvc defined in 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 controller class is mapped to "/ login" ->
<the bean name = "/ login" class = "controller.LoginController" /> // in the bean / login corresponds to the LoginController, i.e., access to the root Url directory / login corresponding to the specific page LoginController
<-! LoginController controller class is mapped to "/ Register" ->
<the bean name = "/ Register" class = "controller.RegisterController" />
</ Beans>

___________________________________________________________________________________________________________________________

5. Release and run:

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

It should appear index.jsp page:

Then click the link to the page jump

The above is spring MVC first use

Great God looked at do not spray! Grateful brother ah!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   

Guess you like

Origin www.cnblogs.com/broadencoder/p/12591467.html