Idea+maven+springmvc

Source: Internet
Author: User

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.

  1. To create an empty Java project, select the Maven module, the name of the project, or whatever it is in the Select type.
  2. In the next page, nothing changed, no choice, dot finish
  3. 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>< Span class= "PLN" > <scope>provided</scope> </dependency> <dependency> <groupid>javax.servlet</groupid>  <artifactid>jstl</artifactid > <version>1.2</version>  </dependency> </dependencies                 
  4. Wait for IntelliJ to automatically import the package that is written inside

  5. 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.
  6. Edit the Web. xml file and change it to the following:

    <?XML version="1.0"Encoding="UTF-8"?><web-app xmlns="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< span class= "tag" ></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.

  7. Servlet-context.xml content 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" 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-scan Base-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--<bean Class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" ><property name= "prefix"  value=  "/web-inf/views/"  /><property name= " suffix " value=". JSP " span class= "tag" >/></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.

  8. 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;@ControllerPublic Class HomeController { @RequestMapping ( "home" )  public string Home (model  Model)  { Modeladdattribute ( "message"   "Hello, world" //return "web-inf/views/home.jsp";  return  "home" ; }}        
  9. 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> span class= "tag" > value=  "${message}" /> again! </body></HTML>              

    Here the use of Jstl, previously written JSP children's shoes, should be seen on the understanding.

  10. 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.

Idea+maven+springmvc

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.