Intellij IDEA 를 사용 하여 Spring MVC 를 0 에서 사용 합 니 다.
13815 단어 intelliJ idea
자바 를 해 킹 한 지 이렇게 여러 해 가 되 었 으 니,오늘 자바 를 위해 글 을 한 편 쓰 자.
이 글 은 자 바 를 처음 접 했 을 때 자바 WEB GUI 개발 에 종사 하고 싶 은 사람들 을 도와 주 고 싶 었 습 니 다.저 에 게 는 자바 로 WEB 를 써 보 겠 다 는 생각 이 들 었 습 니 다.하지만 한 번 도 성공 한 적 이 없습니다.struts 든 순수 JSP 든 항상 설정 이 좋 지 않 아서 tomcat 은 제 프로그램 을 정확하게 실행 할 수 없습니다.
그 이후로 저 는 자바 의 application,특히 tomcat 에 배치 해 야 하 는 것 은 너무 어렵 고 복잡 하 다 고 불평 해 왔 습 니 다.그리고 이것 을 전문 적 으로 말 하 는 문서 도 없 었 습 니 다.모두 eclipse 로 SSH 를 설정 한 다음 에 개발 하고 배치 하 는 방법 을 말 했 습 니 다.eclipse 를 사용 하고 싶 지 않 은 사람 에 게 는 손 쓸 방법 이 없 었 습 니 다.
자,쓸데없는 소리 하지 마 세 요.오늘 제 가 말씀 드 리 고 싶 은 것 은 Srping MVC 의 사용 과 tomcat 의 배치,그리고 어떻게 Maven 으로 dependency 를 관리 하 는 지 입 니 다.제 가 사용 하 는 도 구 는 Intellij IDEA 입 니 다.하지만 프로그램의 디 렉 터 리 구조 가 어떤 지 설명 하 겠 습 니 다.그러면 어떤 IDE 를 사용 하 든 참고 할 수 있 습 니 다.
<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>
<?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>
이 단락 의 내용 은 url 을 appservlet 설정 으로 옮 기 는 것 입 니 다.appservlet 설정 에서 servlet-context.xml 을 사용 합 니 다.<?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/mvc
http://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>
이 단락 의 대략적인 뜻 은 패키지'me.davidx.LearnJava'를 자동 으로 검색 하여 controller 정 의 를 찾 는 것 입 니 다.또한 controller 는@Controller 를 사용 하여 정의 합 니 다.bean 의 정 의 는 모든 View 파일 이/WEB-INF/views 디 렉 터 리 로 찾 아 오고 접 두 사 는.jsp 입 니 다.package me.davidx.LearnJava.controllers;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@ControllerpublicclassHomeController{@RequestMapping("/home")publicString home(Model model){
model.addAttribute("message","hello, world");//return "WEB-INF/views/home.jsp";return"home";}}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html><head><title>Home</title></head><body><h1><c:outvalue="${message}"/></h1><h2>Again!</h2></body></html>
여 기 는 jstl 을 사 용 했 습 니 다.예전 에 jsp 라 고 쓴 어린이 신발 을 한 번 보면 알 수 있 을 것 입 니 다.http://davidx.me,원문 주소:
http://davidx.me/2012/11/18/how-to-use-intellij-to-use-spring-mvc/,원작 자가 공유 해 주 셔 서 감사합니다.