Spring-MVC!

Source: Internet
Author: User

Come to work early in the morning, and there is no network! Nothing can be done!

I remembered reading some spring-MVC documents yesterday.

Configure spring-MVC.

Since there is no network, only official spring documents are available.

1. Create a basic project and then click OK.

2. Introduce the spring-MVC development kit. According to the development documentation, only three packages are required.

The following is a spring document:

Copy libraries'WEB-INF/lib'

First create'lib'Directory in'war/WEB-INF'Directory. Then, from the spring distribution, copyspring.jar
(Fromspring-framework-2.5/dist) Andspring-webmvc.jar(Fromspring-framework-2.5/dist/modules) To the new
'war/WEB-INF/lib'Directory. Also, copy
commons-logging.jar(From
spring-framework-2.5/lib/jakarta-commons) To'war/WEB-INF/lib'Directory. These jars will be deployed to the server and they are also used during the build process.

3. modify web. xml and add a Servlet as follows:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4"         xmlns="http://java.sun.com/xml/ns/j2ee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee          http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >  <servlet>    <servlet-name>springapp</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>springapp</servlet-name>    <url-pattern>*.htm</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>      index.jsp    </welcome-file>  </welcome-file-list></web-app>

4. Create a WEB-INF under the springmvc-servlet.xml. The name here is your project plus servlet.

Then configure the controller you need in the document.

 

<? XML version = "1.0" encoding = "UTF-8"?> <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"> <! -- The application context definition for the springapp dispatcherservlet --> <! -- The controller and the hello.htm path will pass through this controller. It is a bit like configuring action in struts. XML in struts2. Each path corresponds to a cotroller. there is no action concept in srping-MVC, only cotroller. --> <bean name = "/hello.htm" class = "springapp. web. hellocontroller "/> </beans>

5. Create a controller and create hello. jsp under webroot.

package springmvc.web;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class HelloController implements Controller{protected final Log logger = LogFactory.getLog(getClass());public ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {logger.info("run hello Controller");return new ModelAndView("hello.jsp");}}

Enter

Http: // localhost: 8080/springmvc/hello.htm. The hello. jsp page is displayed.

 

 

 

 

 

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.