1. Import SPRINGMVC related jar packages:
2. Add configuration about SPRINGMVC in the Web. XML configuration file
<servlet>
<servlet-name>springmvc</servlet-name>
<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>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3. Add springmvc-servlet.xml config file under src
<!--scan the package and the sub--
<context:component-scan base-package= "test. Springmvc "/>
<!--don ' t handle the static resource--
<mvc:default-servlet-handler/>
<!--If you use annotation must configure following setting--
<mvc:annotation-driven/>
<!--Configure the Internalresourceviewresolver--
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"
Id= "Internalresourceviewresolver" >
<!--prefix configuration back jump to JSP page--
<property name= "prefix" value= "/web-inf/jsp/"/>
<!--suffix--
<property name= "suffix" value= ". jsp"/>
</bean>
4. Create a folder named JSP under the Web-inf folder to hold the JSP view. Create a hello.jsp
5. Create the package and controller as follows:
6. Write the Controller code:
@Controller
@RequestMapping ("/mvc")
public class Mvccontroller {
@RequestMapping ("/hello")
Public String Hello () {
return "Hello";
}
@ResponseBody
@RequestMapping ("/hello2")
Public String Hello2 () {
System.out.println ("Hello2");
return "Hello2";
}
}
Code Description: If you need to return to the JSP page after executing the method, do not add @responsebody on the method;
If the execution method body returns the result of the method, you need to add: @ResponseBody annotations
7. Start the server, type the HTTP://LOCALHOST:8080/project name/mvc/hello
More information: http://www.admin10000.com/document/6436.html
SPRINGMVC Basic Framework Construction