Build Spring MVC from scratch

Source: Internet
Author: User

1. Copy the jar file

2. Populate Web. xml

Write the following in/web-inf/web.xml:

<? XML version="1.0"encoding="UTF-8"?>

<Web-appxmlns:xsi= "/HTTP/ Www.w3.org/2001/XMLSchema-instance " xmlns=< Span style= "COLOR: #2a00ff" > "Http://java.sun.com/xml/ns/javaee" xmlns : Web= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "http ://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " id= "webapp_id" version= "2.5" >

<context-param>

<param-name>contextconfiglocation</param-name>

<param-value></param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</ Listener-class>

</listener>

<servlet>

<servlet-name>x</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</ Servlet-class>

<init-param>

<param-name>contextconfiglocation</param-name>

<Param-value>classpath*:x-servlet.xml</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>x</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</Web-app>

?

Context-param: Specifies the location of the ApplicationContext (bean definition);

The servlet specifies the Servletdispatcher and the servlet configuration file path, and if it is Web-inf, thecontextconfiglocation node Value is defined in the form "/web-inf/xx-servlet.xml" and, if it is a different folder, is specified in classpath form, which means that the servlet configuration file path is Web-inf The latter means to find the configuration file in the compiled folder;

?

3. Filling X-servlet

Create the X-servlet.xml in the Resources folder or the/web-inf/directory and populate the following content:

<? XML version="1.0"encoding="UTF-8"?>

<beansxmlns="Http://www.springframework.org/schema/beans"

???? xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http// Www.springframework.org/schema/mvc "

???? 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 " >

???? <mvc:annotation-driven />

???? <context:component-scanbase-package="Neusoft.monitor.controller"/>

???? <!--jsp View -

???? <beanid="Jspviewresolver"

???????? class="org.springframework.web.servlet.view.InternalResourceViewResolver">

???????? <propertyname="prefix"value="/web-inf/pages/"/ >

???????? <propertyname="suffix"value=". jsp"/>

???? </bean>

</Beans>

Mvc:annotation-driven, the controller is set by means of annotations;

Context:component-scan, representing the package to be scanned for annotations;

<bean id= "Jspviewresolver" > defines how to find a JSP page;

4. Writing a Controller

@Controller

Public class HomeController {

???? @RequestMapping(value = "/home")

???? Public String showhomepage () {

???????? return"Home";

????}

}

Writing home.jsp pages

Write something casually;

5. Try to access

You can see home.jsp content by typing the URL localhost:8080/myspringmvc/home. (Myspringmvc is the name of the project in Eclipse)

?

?

?

?

?

?

?

?

Build Spring MVC from scratch

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.