Spring MVC Primer-Framework Building

Source: Internet
Author: User

I feel that there are not many tutorials on the Internet from the environment, many examples are more complicated, and there is not a diagram of what, so on a simple and understandable version.

How easy is it to pinch? Start with the Eclipse Setup project and make a servlet that responds to the request after the end.

In this article, we need-

Establish an Eclipse project;

Write two XML;

Write two JSP pages;

Write a Java file.

Preparatory work-

Eclipse Java EE version;

Tomcat one;

Note: All italic characters represent--file names, class names, variable names, and so on-replaceable content.

The first step:

Use Eclipse to create a dynamic Web page project.

Enter the project name and click Next.

Click Next.

Select Generate Web. xml and click Finish.

Create a/webcontent/web-inf/views directory for JSP web pages

Create two empty pages, index.jsp and the hello.jsp .

creating Java Packages in Java resources Bob.sun.controller.

in the package, create an empty Indexcontroller class

Create in/webcontent/web-inf/ Springtest-servlet.xml file.

The project's source tree should look like the following:

Step Two:

Copy the jar package.

In the spring MVC Framework, a framework-dependent jar package cannot be added to BuildPath like any other Java project.

Adding a jar package requires copying the jar file directly to/webcontent/web-inf/lib.

Spring MVC relies on several of the following packages.

After the copy is finished, right-click on the project tree.

Step Three:

To open/webcontent/web-inf/web.xml, refer to the following code Editor:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:web= "Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"ID= "webapp_id"version= "3.0">3   <Display-name>Springtest</Display-name> 4   <welcome-file-list>5     <Welcome-file>/web-inf/views/index.jsp</Welcome-file> 6   </welcome-file-list>7   <servlet>8       <Servlet-name>springtest</Servlet-name> 9       <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>Ten       <Load-on-startup>1</Load-on-startup>  One   </servlet> A    -   <servlet-mapping> -       <Servlet-name>springtest</Servlet-name>  the       <Url-pattern>*.do</Url-pattern>  -   </servlet-mapping> -    - </Web-app>

which

<welcome-file-list> Specifies the first page of the entire servlet, which we define as index.jsp.

<servlet> defines the servlet and the classes that are used. Since we are using SPRINGMVC, the Servlet class is assigned to spring's dispatcherservlet.

<servlet-mapping> <url-pattern> Specifies that all requests for ". Do" are handled by the springtest we have just defined. It can be understood that all the ". Do" requests are filtered out and handed to the Springtest servlet.

Fourth Step:

The Spring MVC Framework requires a context configuration file. If there is no definition in Web. XML, Spring MVC searches for a/web-inf that has a name of "[servlet name]-servlet.xml") under this path. The name of our servlet here is springtest, so the default profile name is Springtest-servlet.xml. We did not specify which configuration file was in Web. XML, so spring will go to find springtest-servlet.xml automatically.

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xmlns:p= "http://www.springframework.org/schema/p"5 Xmlns:context= "Http://www.springframework.org/schema/context"6 xsi:schemalocation= "Http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd8 Http://www.springframework.org/schema/context9 http://www.springframework.org/schema/context/spring-context-3.0.xsd ">Ten     One  <Context:component-scanBase-package= "Bob.sun.controller" />    A  <BeanID= "Viewresolver" - class= "Org.springframework.web.servlet.view.UrlBasedViewResolver"> -   < Propertyname= "Viewclass" the value= "Org.springframework.web.servlet.view.JstlView" /> -   < Propertyname= "prefix"value= "/web-inf/views/" />  -   < Propertyname= "suffix"value= ". jsp" /> -  </Bean> + </Beans>

which

<context:component-scan> tells spring which package to scan for Java code with its annotations. What is Spring's comment? We'll see in the Java code below.

For simplicity, this xxx-servlet.xml has only one bean, which specifies which class of V (iew) in MVC is parsed. The prefix and suffix two properties specify the prefix and suffix of the view. So if we want to return a view in Java, we just need to return to the view name. For example, we want to return a/web-inf/views/hello.jsp, because the prefix is specified as/web-inf/view, and the suffix is. JSP, in Java we just need to return a "hello".

Fifth Step:

Writing JSPs

The index.jsp looks like this:

<%@ Page Language="Java"ContentType="text/html; charset=iso-8859-1"pageencoding="iso-8859-1"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head> <title>Spring 3.0 MVC Demo</title></Head><Body> <ahref= "Hello.do">Say Hello</a> </Body></HTML>

<a href= "Hello.do", when the user clicks on the link, a hello.do action is sent to the servlet. According to our definition in Web. XML, "*.do" will be given to our springtest servlet for processing.

Hello.jsp looks like this:

<%@ Page Language="Java"ContentType="text/html; charset=iso-8859-1"pageencoding="iso-8859-1"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=iso-8859-1"><title>Insert Title here</title></Head><Body><H1>Hello Spring</H1>Just a simple printing.</Body></HTML>

It is important to note that at this point in the home page click on the hello.do link will not jump directly to this screen, because we have not written controller.

So

Sixth step:

Write Java code.

Bob.sun.controller.IndexController.java:

Package Bob.sun.controller;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.servlet.modelandview;@ Controllerpublic class Indexcontroller {    @requestmapping("/hello.do")    public Modelandview HelloMethod () {        String ret = ".... you spring";        Modelandview ("Hello", "ret", ret);}    }

Yes, you're right. In this example, Java code is so simple.

which

@Controller This is a spring frame comment that appears in front of the class. It indicates that the entire class is C (Ontroller) in spring. The controller is used to control the view, populate the view data, and so on.

@RequestMapping ("xxx") This is also a comment for the spring framework, which appears in front of the method, indicating that this method responds to the XXX request. You can also add the HTTP method to the back. The details of this section will be placed in the next article.

Since the controller is used to control the view, I return a Modelandview object at the end of this method. The constructor of this class can accept three parameters.

The first argument is a string that indicates which view is returned. You can return to the full path (/web-inf/views/xxx.jsp) of the view. Since we previously specified the prefix and suffix of the view in the servlet configuration file, we only return the name of the view here.

If the object is constructed with two parameters, the second parameter is a map containing the key-value pairs passed to the view, which can be called in the JSP.

If constructed with three parameters, the second parameter is the parameter name passed to the view, and the third is the value of the parameter.

That's all the code is. The remaining step is to configure a Tomcat server.

Seventh Step:

It's easy to configure Tomcat inside Eclipse. Follow the diagram below to take a step-by-step.

First choose to put the project on the server Debug.

Choose Tomcat 7

Select the location and JRE where Tomcat is extracted.

To configure the project, click Finish.

Then Tomcat starts, loads the servlet, and displays our home page. Click the link on the home page and the controller responds and returns to view.

End.

In fact, I was just getting started, two days to summarize the documents, and pull out so a blog.

There's a lot of things not said. Spring Comments (@Component), parameter passing (@RequestParam), Tiles, etc., put it together next time.

Spring MVC Primer-Framework Building

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.