Spring MVC 04 - @Controller,@RequestMapping

We continue the explanation from the previous article.

In the previous article, we achieved printing "Hi, this is first spring mvc application" in the browser without using annotation. Here we use @Controller and @RequestMapping to achieve the same function, which can be compared in this process. Different before and after.

Previous articleWe achieved it in four steps

1. Modify web.xml

2. Add the spring-dispatcher-servlet.xml file to the WEB-INF directory

3. Create a new HelloController.java class in the src folder

4. Create a new HelloPage.jsp page in the WEB-INF directory

Below we will explain the process of using @Controller and RequestMapping one by one:

1. The web.xml file remains the same

2. spring-dispatcher-servlet.xml

before fixing

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<bean name="/welcome" class="com.haha.HelloController"/>

<bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix">
      <value>/WEB-INF/</value>
   </property>
   <property name="suffix">
      <value>.jsp</value>
   </property>
</bean>
</beans>

after modification

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
         
<context:component-scan base-package="com.haha"/>

<bean id="viewResolver" 
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix">
      <value>/WEB-INF/</value>
   </property>
   <property name="suffix">
      <value>.jsp</value>
   </property>
</bean>
</beans>

3. HelloController.java

before fixing

package com.haha;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController {

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
ModelAndView modelandview = new ModelAndView("HelloPage");
   modelandview.addObject("welcomeMessage","Hi, this is first spring mvc application");
   return modelandview;
}

}

after modification

package com.haha;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class HelloController{


@RequestMapping("/welcome")
public ModelAndView helloWorld(){
// TODO Auto-generated method stub
ModelAndView modelandview = new ModelAndView("HelloPage");
   modelandview.addObject("welcomeMessage","Hi, this is first spring mvc application");
   return modelandview;
}


}

4. HelloPage.jsp remains unchanged

After the above modification, start tomcat and enter http://localhost:8080/FirstSpringMVCPro/welcome in the browser

********************@RequestMapping further explanation ************************

The content in the brackets of @RequestMapping() is the basis for the DispatcherServlet to find the path. If we add @RequestMapping(/xx) above the class, then all access to the class must be.../xx/...

For example, in the above example, we add @RequestMapping("/haha") under HelloController.java @Controller, then the content that can be accessed by http://localhost:8080/FirstSpringMVCPro/welcome must be http://localhost :8080/FirstSpringMVCPro/haha/welcome to access.

package com.haha;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller

@RequestMapping("/haha")

public class HelloController{


@RequestMapping("/welcome")
public ModelAndView helloWorld(){
// TODO Auto-generated method stub
ModelAndView modelandview = new ModelAndView("HelloPage");
   modelandview.addObject("welcomeMessage","Hi, this is first spring mvc application");
   return modelandview;
}


}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325988563&siteId=291194637