Experience spring 3 MVC and replace Struts2
10:32:20 let me say a few words
Add to favorites I want to contribute
In Java's WEB framework, Struts2 should be the most famous. However, I recently tried spring 3 MVC. It seems so nice. It is almost as comfortable as ASP. Net MVC3 and will be used later. Simply record the process and there is no technical content.
1. Prepare the package
The download is spring framework 3.2.0, from which the following jar is extracted to the project's WEB-INF/lib:
Spring-beans-3.2.0.RELEASE.jar
Spring-context-3.2.0.RELEASE.jar
Spring-core-3.2.0.RELEASE.jar
Spring-expression-3.2.0.RELEASE.jar
Spring-web-3.2.0.RELEASE.jar
Spring-webmvc-3.2.0.RELEASE.jar
In addition, several third-party jar packages are required to record logs and process json:
Commons-logging-1.1.1.jar
Jackson-core-als-1.9.11.jar
Jackson-mapper-asl-1.9.11.jar
2. WEB-INF/web. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns: 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_2_5.xsd"
Id = "WebApp_ID" version = "2.5">
<! -- Site name -->
<Display-name> mvc </display-name>
<! -- Specify the spring configuration file -->
<Context-param>
<Param-name> contextConfigLocation </param-name>
<Param-value>/WEB-INF/spring-servlet.xml </param-value>
</Context-param>
<Listener>
<Listener-class> org. springframework. web. context. ContextLoaderListener </listener-class>
</Listener>
<Servlet>
<! -- Servlet name, random -->
<Servlet-name> spring </servlet-name>
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet-mapping>
<! -- Servlet name -->
<Servlet-name> spring </servlet-name>
<! -- Intercept all requests, there will be problems with static files, solve in the spring-servlet.xml -->
<Url-pattern>/</url-pattern>
</Servlet-mapping>
<Welcome-file-list>
<Welcome-file> index.htm </welcome-file>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>
3. WEB-INF/spring-servlet.xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: p = "http://www.springframework.org/schema/p"
Xmlns: mvc = "http://www.springframework.org/schema/mvc"
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-3.0.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-3.0.xsd>
<! -- Start the annotation-driven Spring MVC function and register the ing between the request url and the annotation POJO class method -->
<Mvc: annotation-driven/>
<! -- Enable the package scan function to register classes with annotation such as @ Controller, @ Service, @ repository, and @ Component to become spring beans -->
<Context: component-scan base-package = "com. test. mvc. web"/>
<! -- Analysis of Model View name, find the corresponding jsp file under the WEB-INF/jsp directory -->
<Bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver" p: prefix = "/WEB-INF/jsp/" p: suffix = ". jsp"/>
<! -- Release static files in/scripts -->
<Mvc: resources mapping = "/scripts/**" location = "/scripts/"/>
</Beans>
4. WEB-INF/applicationContext. xml
Spring configuration file, because we do not use its other functions, it is good to temporarily put a blank one.
<? 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: util = "http://www.springframework.org/schema/util"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/util
Http://www.springframework.org/schema/util/spring-util-3.0.xsd>
</Beans>
5. Write Controller
Package com. test. mvc. web;
Import org. springframework. stereotype .*;
Import org. springframework. web. bind. annotation .*;
Import org. springframework. web. servlet .*;
/**
* Controller, annotated with Controller
*/
@ Controller
Public class HomeController {
/**
* Map to/welcome
*/
@ RequestMapping (value = "/welcome ")
Public ModelAndView welcome (){
ModelAndView mv = new ModelAndView ("welcome"); // use welcome. jsp. If you do not write it, the url is also welcome. jsp by default.
Mv. addObject ("hello", "Hello"); // Add a string named hello to the model.
Client client = new Client ();
Client. setName ("User ");
Mv. addObject ("client", client); // Add a custom object named client.
Return mv;
}
/**
* If you do not need a Model, it is easier to return the String directly. The corresponding view is login_page.jsp.
*/
@ RequestMapping (value = "/login ")
Public String login (){
Return "login_page ";
}
/**
* A json return method identified by ResponseBody
* You can define parameters in the url. It is really easy to implement RESTful.
* Parameter passing is flexible. parameters can be retrieved from URLs or defined as common parameters.
*/
@ RequestMapping (value = "/client/{name}", method = RequestMethod. GET)
@ ResponseBody
Public Client getClient (@ PathVariable String name, String title ){
Client client = new Client ();
Client. setName (title + "" + name );
Return client;
}
}
It uses the Client, which is a very simple POJO:
Package com. test. mvc. web;
/**
* Customize a POJO
*/
Public class Client {
Private String name;
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
}
6. Write View
Depending on the configuration in the spring-servlet.xml, the view is placed under the WEB-INF/jsp to create welcome. jsp:
<% @ 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> Spring MVC </title>
<Script src = "scripts/jquery-1.4.2.js"> </script>
<Script>
$ (Function (){
$ ("# BtnGet"). click (function (){
$. Ajax ({
Type: 'get ',
Url: 'client/tiany', // pass the name parameter through the url
DataType: 'json ',
Data: {title: "Mr"}, // pass the title parameter through data
Success: function (data ){
Alert (data. name );
},
Error: function (data ){
Alert (data. responseText );
}
});
});
});
</Script>
</Head>
<Body>
<! -- Display the hello string in the model and the name of the client object -->
$ {Hello}
$ {Client. name}
<Br/>
<Input id = "btnGet" type = "button" value = "get client"/>
</Body>
</Html>
Everything is ready. Run Tomcat. Visit localhost: 8080/mvc/welcome in a browser to see the page.