Intellij IDEA Ultimate Spring MVC project to create a stepped pit remember

Students can apply for Intellij IDEA Ultimate Free Trial! I finally can temporarily do not toss Community Edition la la la la la! ! !

IDEA Ultimate Spring MVC projects can be created directly, but after the project is not directly created to run, need to do some configuration.

First, create a project

Open Intellij IDEA, create a project (Create New Project);

On the left choose "Spring", and then check the right side of "Spring MVC", following "Web Application" should be automatically checked, and if not, check the manual;

Below keep the default option "Download" you can click on "Next";

Enter a project name (Project name), for example, here I am "SpringMVCTest", other do not control, click "Finish";

After waiting IDEA download response packet;

We get an initial project structure:

At this point we press the shortcut keys Ctrl + Alt + Shift + S, open the project structure (Project Structure), you can see there are two Problems, click on the "Problems", and on the right click any of the blue "[Fix]", select "Add all misssing dependencies of 'XXX' to the artifact" in the sub-menu, click "OK".

In this way, when the compiler is running, it will in the "project folder \ out \ artifacts \ project name _war_explodedlib \ WEB-INF" is also to create a lib folder and copy the relevant past packet.

Second, configure Tomcat

Click on the top right corner of the "Add Configuration ..."

Top right, click the plus sign, choose "Tomcat Server" in the menu, and select "Local" in the submenu;

Modify "Name", such as this is "Tomcat 9";

Specifies the Tomcat directory (Application server);

Select the default browser opens in "After launch" in;

Below you can see a "Warning", click on the right of "Fix";

Then automatically jumps to "Deployment", and automatically adds the "XXX: war exploded";

For convenience, we will project into the following application context name like (for example, where "/ SpringMVCTest"), Server tab side of the "URL" will automatically become "http: // localhost: 8080 / application context "form (for example here will become" http: // localhost: 8080 / SpringMVCTest "). Click "OK".

In this case, click on the upper right corner of the green triangle run the project;

After starting Tomcat, IDEA will automatically starts the browser open "http: // localhost: 8080 / SpringMVCTest", the display is index.jsp web folder under the.

Third, configure View Resolver and Controller

1, package introduced jstl

download link:

https://search.maven.org/remotecontent?filepath=javax/servlet/jsp/jstl/javax.servlet.jsp.jstl-api/1.2.2/javax.servlet.jsp.jstl-api-1.2.2.jar

After downloading copy to the lib file in the project folder;

Open again Project Structure, click the "Modules", in the middle of the selected item, click the plus sign to the right, select the first item "1 JARs or directories ...";

We choose just copied into the lib folder in a jar, and click "OK";

 

Problems then prompt problem later, click the "Problems", right click the blue "[Fix]", select "Add 'javax.servlet.jsp.jstl-api-1.2.2.jar' in the submenu to the artifact ", click" OK ".

2, the new jsp page

In the web \ WEB-INF folder, create a new folder named "jsp", and in which a new jsp file named "test.jsp", as shown:

 3, the new controller class

In the src folder to create the package, the package called "reverse domain name + project + controller" format, such as my domain is "zhouxy.xyz", the project name is "SpringMVCTest", so here I package named "xyz. zhouxy.springmvctest.controller "(in fact, the best should be" xyz.zhouxy.springmvc.test.controller ").

Created inside a Java class, my class here called "MyController", as shown, for the class plus @Controller notes, create a method in the class, with @RequestMapping ( "/ test"), the "/ test" and the method binding. Method returns the string "test".

 4, modify the configuration file

Open the web.xml, org.springframework.web.servlet.DispatcherServlet the corresponding mapping to "/", so that it responds to all url accessing the web application (i.e., in response to "http: // localhost: 8080 / SpringMVCTest /", regardless of what later added). Figure:

Here default servlet-name for the "dispatcher", the corresponding Servlet configuration file called "dispatcher-servlet.xml". If you change, then the file name should be changed to respond to "XXX-servlet.xml" form. I am here not amend it.

Open dispatcher-servlet.xml, modified as shown below:

Note, where <context: component-scan /> base-package attribute value element is the complete package our controller package, if your package name and I do not like attention to be modified to be consistent.

When following View Resolver, the method of the controller class returns the string "test", automatically add "/ WEB-INF / jsp /" and ".jsp" in front and rear, respectively, to become "/ WEB-INF / jsp / test.jsp ".

Run the project again, as before the browser opens the "http: // localhost: 8080 / SpringMVCTest /", we enter the "test" in its back, Dispatcher will <context: component-scan /> element specified in the package find @Controller annotated with class, and where found with @RequestMapping ( "/ test") annotation request processing method, and then executed. As described above, our method returns the string "test", the View Resolver "processing" into "/WEB-INF/pages/test.jsp", is returned to the org.springframework.web.servlet.DispatcherServlet Spring objects, web.xml is configured dispatcher, made it to jump to the "/WEB-INF/pages/test.jsp" page.

 

Guess you like

Origin www.cnblogs.com/Luquan/p/12483975.html