SPRINGMVC Basic Framework Construction

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.