Using IntelliJ idea to develop SPRINGMVC website (ii)

Source: Internet
Author: User

Note: This article undertakes the previous article: using IntelliJ idea to develop SPRINGMVC website

Five, SPRINGMVC framework configuration

Finish the above configuration, that means that the basic development environment has been set up, now to start SPRINGMVC website development.

1. Web. XML configuration

Open the Web. xml file under src\main\webapp\web-inf\ and slightly update the version of Web. XML to support some more advanced syntax, as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://xmlns.jcp.org/xml/ns/javaee/http Xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "version=" 3.1 "> <display-name>springmvcdemo Web Applicatio N</display-name></web-app>

Add a servlet to <web-app>:

<?xml version= "1.0"  encoding= "UTF-8"? ><web-app xmlns= "http://xmlns.jcp.org/xml/ns/ Java ee "         xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance "         xsi:schemalocation=" http// Xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "          version= "3.1" >    <display-name>SpringMVCDemo  web application</display-name>    <servlet>         <servlet-name>mvc-dispatcher</servlet-name>         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>         <load-on-startup>1</load-on-startup>     </servlet>   &nbsP;<servlet-mapping>        <servlet-name>mvc-dispatcher </servlet-name>        <url-pattern>/</url-pattern>     </servlet-mapping></web-app>

The servlet, named Mvc-dispatcher (name modifiable), is used to intercept requests (Url-pattern to/, to intercept all requests) and to be handled by the spring MVC backend controller. This configuration is required.

In order to be able to handle the Chinese post request, and then configure a encodingfilter to avoid the POST request Chinese garbled situation:

<filter> <filter-name>encodingFilter</filter-name> <filter-class> Org.springframework.web.filter.characterencodingfilter</filter-class> <init-param> <param-name&gt ;encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param&        Gt <param-name>forceEncoding</param-name> <param-value>true</param-value> &LT;/INIT-PARAM&G T;</filter><filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern >/*</url-pattern></filter-mapping>

At this point, the Web. XML configuration is complete.

2. Xxx-servlet.xml Configuration

After configuring Web. XML, you need to create a new mvc-dispatcher-servlet.xml in the Web. Xml Sibling Directory (-servlet is preceded by the servlet name defined in the servlet):

After you create the XML file, click Configure in the upper-right corner, the Setup frameworks interface appears, click OK, so that IntelliJ idea recognizes the SPRINGMVC configuration file:

The Mvc-dispatcher-servlet.xml file is as follows:

<?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.xsd "></beans>

The MVC framework consists of three parts: model, view and controller. The model is typically used by some basic Java Bean,view for the corresponding page display, and the controller is used to process Web site requests.

Create a new package to save the controller in Src\main\java:

Create a new Java class Maincontroller in the controller package (the name is not fixed and can be arbitrarily taken), and modify the following:

Package Com.gaussic.controller;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.requestmethod;/** * Created by Dzkan on 2015/10/3. */@Controllerpublic class Maincontroller {@RequestMapping (value = "/", method = requestmethod.get) public String in    Dex () {return ' index '; }}

(1) @Controller Note: The use of annotations, you can clearly define the class to process the request of the Controller class;

(2) Note @RequestMappind (): Used to define a request map, value is the requested URL, the values are/description, the Request first page request, method to specify the request type, usually get and post;

(3) Return "index": the page returned after the request has been processed, and this request returns to the Index.jsp page.

Go back to Mvc-dispatcher-servlet.xml and make the relevant configuration. First, add the Component-scan tag, indicate the package where the controller resides, and scan the annotations:

<!--Specify the controller's package and scan for annotations--><context:component-scan base-package= "Com.gaussic.controller"/>

Then the configuration of static resources such as JS, image, CSS and so on, so that SPRINGMVC can access static resources within the site:

<!--access to static resources (JS, image, etc.)--><mvc:default-servlet-handler/>

The SPRINGMVC annotation mode is switched on, and since we use the annotation method to define the definition, we can save a lot of configuration:

<!--opening annotations--><mvc:annotation-driven/>

Then the configuration of the View parser:

<!--viewresolver View parser--><!--used to support Servlets, JSP view resolution--><bean id= "Jspviewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" viewclass "value=" Org.springframework.web.servlet.view.JstlView "/> <property name=" prefix "value="/web-inf/pages/"/> < Property name= "suffix" value= ". JSP"/></bean>

Detailed instructions are required on how the controller can find the view file. In one of the controller's methods, the returned string defines the name of the JSP you want to access (such as index above). In Jspviewresolver, there are two properties, one is prefix, a file path prefix is defined for the required access, and the other is a suffix that represents the suffix of the file to be accessed, which is the. jsp. Then, if the return string is XXX, SPRINGMVC will find the/web-inf/pages/xxx.jsp file.

When the above configuration is complete, the Mvc-dispatcher-servlet.xml file looks like this:

<?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:context= "Http://www.springframework.org/schema/context"         xmlns:mvc= "Http://www.springframework.org/schema/mvc"         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 ">    <!--Specify the package for  controller , and scan the note-->    <context: Component-scan base-package= "Com.gaussic.controller"/>    <!--  Static resources (JS, Image, etc.) Access  -->    <mvc:default-servlet-handler/>    < !--viewresolver  View parser-->    <!--used to support servlet, JSP view parsing-->     <bean id= "Jspviewresolver"  class= " Org.springframework.web.servlet.view.InternalResourceViewResolver ">         <property name= "Viewclass"  value= "Org.springframework.web.servlet.view.JstlView"/>         <property name= "prefix"  value= "/WEB-INF/pages/"/ >        <property name= "suffix"  value= ". jsp"/>     </bean></beans>

We delete the index.jsp file under the WebApp directory, create a new folder in the Web-inf directory pages, create a new index.jsp in the pages directory, and modify it as follows:

<%@ page contenttype= "Text/html;charset=utf-8"  language= "java"  %><! doctype html>

Now, you need to configure Tomcat to run the project. Click the down arrow in the upper right corner of the interface to select Edit configurations:

Click on the "+" sign in the upper-left corner, select Tomcat Server, (if not, select the + items to find Tomcat server), and then select Local:

Enter the following interface:

Click Configure on the right side of application server to import the Tomcat directory:

After configuring the path to Tomcat, as shown, there is still a warning, and there is an error mark on the left Tomcat7 icon stating that it is not fully configured:

We also need to deploy the project to the Tomcat server. Click Deployment, and then click on the "+" sign on the right to add a artifact:

Select the second: War exploded, click OK, and the project is already deployed to Tomcat:

Click OK again and the entire Tomcat configuration ends:

Click the green arrow in the Red box in the upper right corner of the interface to start Tomcat, and its console output will be displayed below IntelliJ

After launch, the browser will automatically popup the project home page:

This explains that the configuration is complete. Here's how it works: first, the browser accesses the localhost:8080, the backend controller intercepts the request, handles it accordingly (none here), and displays it in the jump to view index.jsp. Thereafter, a detailed introduction will be made.

(To be Continued ...) )


Using IntelliJ idea to develop SPRINGMVC website (ii)

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.