Original: Using IntelliJ idea to use spring MVC from zero
Using IntelliJ idea to use spring MVC from zero
Black Java for so many years, write an article for Java today.
This article is mainly to help those who have just come into contact with Java, but also want to engage in Java WEB GUI development. For me, I had a long time to try to write the web with Java idea, but did not succeed, whether it is with struts, or pure JSP, is always poorly configured, Tomcat does not run my program correctly.
Since then, I've been complaining about Java's application, especially the deployment of Tomcat, too difficult, too complicated. And there's no documentation specifically about this, it's all about how to configure SSH with Eclipse, then develop, then deploy. For a person who doesn't want to use eclipse, it's impossible.
Well, no more nonsense, today I'm going to talk about the use of srping MVC and the deployment of Tomcat, and how to manage dependency with Maven. The tool I use is IntelliJ idea, but I'll explain what the directory structure of the program is, so you can refer to whatever IDE you use.
- To create an empty Java project, select the Maven module, the name of the project, or whatever it is in the Select type.
- In the next page, nothing changed, no choice, dot finish
The default opens the Pom.xml, this is MAVEN's configuration file, in order to allow the program to automatically pour into spring mvc and JSP, Selvlet and other dependencies, we add the following configuration:
<properties><java-version>1.7</java-version><org.springframework-version>3.1.3.RELEASE</org.springframework-version></properties><dependencies><!--Spring --<dependency><groupId>org.springframework</groupId><artifactId>Spring-context</artifactId><version>${org.springframework-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>Spring-webmvc</artifactId><version>${org.springframework-version}</version></dependency><!--servlet jsp and Jstl -<dependency><groupId>Javax.servlet</groupId><artifactId>Javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>Jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency><dependency><groupId>Javax.servlet</groupId><artifactId>Jstl</artifactId><version>1.2</version></dependency></dependencies>
Wait for IntelliJ to automatically import the package that is written inside
- Make sure that we have a web directory that is the same as the SRC directory, and then a directory called Web-inf. Here, let's talk about the WEBAPP directory structure of Tomcat. First, there is a web-inf directory, and then in Web-inf, there is classes directory: The directory where all the code is stored; Lib directory: store all jar files; Web. xml file, storing the WebApp configuration.
Edit the Web. xml file and change it to the following:
<?XML version="1.0"encoding="UTF-8"?><web-appxmlns="Http://java.sun.com/xml/ns/javaee"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><!--Handles All requests into the application-<servlet><servlet-name>Appservlet</servlet-name><servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextconfiglocation</param-name><param-value>/web-inf/spring/appservlet/servlet-context.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>Appservlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
The content of this section is to jump the URL to the Appservlet configuration, in the Appservlet configuration, using Servlet-context.xml.
Servlet-context.xml content is as follows:
<?XML version="1.0"encoding="UTF-8"?><beansxmlns="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/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd "><!--Scans the classpath of this application for @Components to deploy as beans--<context:component-scanBase-package="Me.davidx.LearnJava"/><!--Configures the @Controller programming model --<mvc:annotation-driven/><!--resolves view names to protected. JSP resources within The/web-inf/views directory--<beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyname="prefix"value="/web-inf/views/"/><propertyname="suffix"value=". JSP"/></bean></beans>
The approximate meaning of this paragraph is that the Automatic scan package "Me.davidx.LearnJava" to find the controller definition. At the same time, the controller is defined using @controller. The definition in the bean indicates that all view files are found in the/web-inf/views directory, and the suffix is. jsp.
In the Me.davidx.LearnJava package, define a HomeController class, which gives an example:
PackageMe.Davidx.Learnjava.Controllers;Importorg.springframework.stereotype.Controller;Importorg.springframework.UI.Model;Importorg.springframework.Web.Bind.annotation.requestmapping;@Controller PublicclassHomeController{@RequestMapping("Home") PublicStringHome(ModelModel){Model.AddAttribute("Message","Hello, World");//return "web-inf/views/home.jsp";return"Home";}}
After reading the controller definition, let's look at the view file: home.jsp:
<%@taglib URI="Http://java.sun.com/jsp/jstl/core"prefix="C"%><%@Page Session="false"%><title>Home</title><body>value="${message}"/>again!</body>
Here the use of Jstl, previously written JSP children's shoes, should be seen on the understanding.
- The code work is done, and here is the deployment.
This article is from: Http://davidx.me, the original address: http://davidx.me/2012/11/18/how-to-use-intellij-to-use-spring-mvc/, thank the original author to share.
Using IntelliJ idea to use spring MVC from zero