Spring: WEB (2) Hello, World!

Source: Internet
Author: User

Before writing code, you must import the following necessary libraries to the project:
Spring-framework-2.5.6 \ dist \ spring. jar
Spring-framework-2.5.6 \ dist \ modules \ spring-webmvc.jar
Spring-framework-2.5.6 \ lib \ jakarta-commons \ commons-logging.jar
Spring-framework-2.5.6 \ lib \ j2ee \ jstl. jar

Just copy these files to the project's WebContent \ WEB-INF \ lib directory, and then refresh the project in eclipse, You can automatically import the database.

At this point we can start modifying the web. xml configuration file under the WebContent \ WEB-INF folder of the project.
To use Spring to create a web, we must first create a web. create a servlet in xml. The servlet is used to tell the tomcat container that Spring is used to process the request when a request conforms to a certain rule.

For example, if we want Spring to take over all url requests suffixed with. do, we need to modify web. xml to the following content:
Web. xml:
Xml Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app id = "WebApp_ID" 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">
<Display-name> SpringWeb </display-name>
<Welcome-file-list>
<Welcome-file> index.html </welcome-file>
<Welcome-file> index.htm </welcome-file>
<Welcome-file> index. jsp </welcome-file>
<Welcome-file> default.html </welcome-file>
<Welcome-file> default.htm </welcome-file>
<Welcome-file> default. jsp </welcome-file>
</Welcome-file-list>
<Servlet>
<Servlet-name> dispatcherServlet </servlet-name>
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> dispatcherServlet </servlet-name>
<Url-pattern> *. do </url-pattern>
</Servlet-mapping>
</Web-app>


If you have the servlet programming basics, you can know that the servlet-name node content can be customized, however, the <servlet> and the corresponding <servlet-mapping> must have the same servlet-name. They represent the definition of a servlet and the servlet ing relationship between the servlet.

The role of this servlet is to tell tomcat that as long as the url request is. if do is a suffix, submit it to org. springframework. web. servlet. dispatcherServlet is a Spring class, And DispatcherServlet can understand its function as a distributor.

 

After successful configuration in web. xml, we can officially enter the spring section. First we have to create a new dispatcherServlet-servlet.xml file in the same directory of web. xml.

Of course you can create a new xml file name in any directory, and usually put in a new custom folder under the WEB-INF folder, generally WEB-INF \ conf folder. However, you have to modify the servlet parameters in the web. xml file.
Here, you can use this content as a self-taught content to google. Learning google is more important than learning programming!

So by default, is the xml file name must be dispatcherServlet-servlet.xml? The answer is yes.
By default, Spring will first automatically search for the xml file in the same directory of web. xml with the name of the servlet followed by-servlet.
At this time, you will find that the front part of the dispatcherServlet-servlet.xml file name is the servlet-name value in web. xml, and the second half is the constant servlet word.
If you have previously customized servlet-name, for example, changed to bolide74Servlet, then the file name here must be a bolide74Servlet-servlet.xml.

DispatcherServlet-servlet.xml:
Xml Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: p = "http://www.springframework.org/schema/p"
Xmlns: context = "http://www.springframework.org/schema/context"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd>
<! -- View -->
<Bean id = "viewResolver"
Class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<! -- Use the tag library, which is not required for the moment -->
<! -- <Property name = "viewClass" value = "org. springframework. web. servlet. view. JstlView"/> -->
<Property name = "prefix" value = "/WEB-INF/jsp/"/>
<Property name = "suffix" value = ". jsp"/>
</Bean>
 
<! -- Ing -->
<Bean id = "urlMapping"
Class = "org. springframework. web. servlet. handler. SimpleUrlHandlerMapping">
<Property name = "mappings">
<Props>
<Prop key = "helloWorld. do"> helloWorld </prop>
</Props>
</Property>
</Bean>
 
<! -- Action -->
<Bean id = "helloWorld" class = "com. iteye. bolide74.action. HelloWorld">
<Property name = "viewPage" value = "helloWorld"/>
</Bean>
</Beans>


You will find that this dispatcherServlet-servlet.xml is actually a Spring configuration file, which is one by one bean, very kind!
ViewResolver the role of this bean is to tell Spring our web View files are with. jsp as the suffix, that is, jsp page, they are stored in the/WEB-INF/jsp/this folder. As long as this bean is available, you don't need to bother writing the path and file name of the jsp page every time.
UrlMapping is a ing table that tells Spring which url request corresponds to which action. For example, we will tell Spring to call the helloWorld bean as long as the url request is helloWorld. do.
I will not explain more about the helloWorld bean. Its Injection parameter is viewPage and its value is helloWorld. The value of helloWorld represents the view file corresponding to this action as/WEB-INF/jsp/helloWorld. jsp, because we have set the path and Suffix in viewResolver, we only need to enter helloWorld here.

Finally, we sum up the role of this xml, is to tell Spring our view file to store all the jsp files in the/WEB-INF/jsp/directory, and set when the url request is helloWorld. call the bean helloWorld to process the do operation, and inject the value helloWorld into the viewPage parameter.

 

Next we will create the action class com. iteye. bolide74.action. HelloWorld:

HelloWorld. java:
Java code
Package com. iteye. bolide74.action;
 
Import java. util. HashMap;
Import java. util. Map;
 
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
 
Import org. springframework. web. servlet. ModelAndView;
Import org. springframework. web. servlet. mvc. Controller;
 
Public class HelloWorld implements Controller {
Private String viewPage;
 
Public String getViewPage (){
Return viewPage;
}
 
Public void setViewPage (String viewPage ){
This. viewPage = viewPage;
}
 
 
@ Override
Public ModelAndView handleRequest (HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
String msg = arg0.getParameter ("msgValue ");
Msg = "your input value is" + msg;
Map model = new HashMap ();
Model. put ("msg", msg );
Return new ModelAndView (getViewPage (), model );
}
}


This class implements the Controller Interface, which is provided by Spring. It actually acts as a proxy. Do you still remember the previous AOP proxy? If you forget it, you can review it later.

This class has a viewPage member attribute and uses set/get injection. You can also use custom attributes. Its value represents the file name of the View File corresponding to this action, which we have mentioned before.

The Controller Interface has a handleRequest abstract method, whose type is ModelAndView. This type is provided by Spring and its name is its function. I will introduce this type in detail later. Simply put, this class contains the parameter data and view content of a url request. If you know MVC, this class contains M and V, and the Controller Interface is the C.
Here we embed a msg parameter in the "original Model" in the handleRequest method, whose value is the value of msgValue obtained by HttpServletRequest.


Finally, create a new helloWorld. jsp View File in the/WEB-INF/jsp/directory:
HelloWorld. jsp:
Html code
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Insert title here </title>
</Head>
<Body>
<Form action = "helloWorld. do">
<Input type = "text" name = "msgValue" value = ""/>
<Input type = "submit" value = "speak"/>
</Form>
<% = Request. getAttribute ("msg") %>
</Body>
</Html>

I want to understand this. It is a traditional jsp page. There is a form in it. Have you noticed the text input box with name msgValue? The role of HelloWorld. java above is the msgValue value here, which is processed and then returned to the client.

 

Okay, it's done. Let's start F11 directly. Note that it's Run on Server. At this time, eclipse will create a new built-in browser tag. The url address in the browser is http: // localhost: 8080/SpringWeb/WEB-INF/jsp/helloWorld. jsp, but the page content shows Error 404.
This is because of tomcat's security mechanism, the client is unable to directly access the WEB-INF folder and all the files under it, therefore, we will put all the important configuration files, view source code and so on under this folder.

To see that the job is successful, you must enter the URL http: // localhost: 8080/SpringWeb/helloWorld in the address bar. do

 

In the input box, enter Hello, World! Click the Speak button to see "Hello, World !"
Congratulations, I finally got started with Spring Web programming.

 


Finally, a problem is thrown out. When you enter the Chinese "Hello!" in the input box !" When the page returns to you, the content is garbled. What should I do? This is the problem that must be solved in the next article. You can google it first.

Related Article

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.