Spring MVC 입문 - 프로젝트 구축 절차 분석
12820 단어 【SSM】
이곳 은 Maven 프로젝트 관리 도 구 를 사용 하여 SpringMVC 프로젝트 를 관리 합 니 다.
STEP 1: Maven 설치 및 프로젝트 구축
자세 한 절 차 는 Maven 편 참조:http://blog.csdn.net/csdn_terence/article/details/53517287
STEP 2: Spring MVC 프로젝트
1. pom. xml 파일 설정
첫째, 속성 에서 일부 소프트웨어 패키지 의 버 전 을 설명 합 니 다. 이 유 는 공사 가 복잡 하고 방대 하면 이 설정 을 통 해 가방 에 의존 하 는 버 전 을 비교적 명확 하 게 지도 하여 우리 가 다른 것 을 처리 하 는 데 편리 하도록 할 수 있 습 니 다.
-->
properties>
dependency>
dependencies>
dependencyManagement>
셋째, 의존 하 는 패 키 지 를 넣는다.
-->
dependency>
dependency>
dependencies>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
2. 웹. xml 파일 설정
xmlversion="1.0"encoding="UTF-8"?>
xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
ArchetypeCreated Web Application
contextConfigLocation
/WEB-INF/configs/spring/applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml
1
mvc-dispatcher
/
3. applicationContext. xml 파일 설정
applicationContext 는 Spring 컨 텍스트 와 관련 된 프로필 입 니 다. 이 파일 은 전체 응용 에서 유 니 버 설 구성 요소 가 공동으로 사용 하 는 bean 관 리 를 구성 하고 BeanFactory 인 터 페 이 스 를 계승 합 니 다. BeanFactory 의 모든 기능 을 포함 하 는 것 을 제외 하고 국제 화 지원, 자원 방문, 사건 전파 등에 서 좋 은 지원 을 했 습 니 다.
그 중에서 applicationContext 의 로드 실현 은 두 가지 방식 으로 각각 ContextLoader Listener 와 ContextLoader Servlet 이다.
4, DispatcherServlet. xml 파일 설정
xmlversion="1.0"encoding="UTF-8"?>
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">
context:component-scan>
text/html;charset=UTF-8
application/json;charset=UTF-8
5. 제어 층 컨트롤 러
@Controller
@RequestMapping("/courses")
public class CourseController {
private static Logger log=LoggerFactory.getLogger(CourseController.class);
private CourseServicecourseService;
@Autowired
public void setCourseService(CourseService courseService) {
this.courseService=courseService;
}
@RequestMapping(value="/view",method=RequestMethod.GET)
public String viewCourse(@RequestParam("courseId") Integer courseId,Model model) {
Course course=courseService.getCourseById(courseId);
model.addAttribute(course);
System.out.println(courseId);
return"course_overview";
}
해명 하 다.
l @Controller, 알림 컨 텍스트, 이 종 류 는 Controller 입 니 다. 접근 제어 에 사용 되 며, 표지 Controller 가 설명 한 후 Spring 의 Dispatcher Servlet 컨 텍스트 에 의 해 관리 되 고 의존 주입 이 완 료 됩 니 다.
l @RequestMapping ("/ courses") 은 클래스 등급 의 annotation 매 핑 주 해 를 통 해 어떤 종류의 url 을 반영 해 야 하 는 지 표시 한 다음 클래스 에 매 핑 하 는 방법 입 니 다.이 맵 은 루트 디 렉 터 리 url 에 있 는 모든 / courses / * 를 처리 합 니 다. 이러한 url 은 차단 되 어 있 습 니 다.
l @Autowired, 성명 자동 실행, 구성원 변수, 방법 과 구조 함 수 를 표시 하여 자동 조립 작업 을 완성 할 수 있 습 니 다.
l @RequestMapping (value = "/ view", method = RequestMethod. GET), 업무 방법 은 표지 조회 내용 에 따 른 업무 논 리 를 제공 하고 Annotation 주 해 를 통 해 클래스 에 매 핑 하 는 방법 에 매 핑 된 클래스 에 맞 춰 요청 을 완성 합 니 다.
l 이 성명 방법 은 처리 할 것 이다.http://localhost:8080/courses/view?courseId=123유형의 요청.
l public String viewCourse(@RequestParam("courseId") Integer courseId,Model model) { }
@RequestParam ("courseId") 은 길 삼 courseId 를 방법 중의 형 삼 courseId 에 연결 하 는 데 사용 되 며, 이 중 Model 은 SpringMVC 특유 의 유형 으로 포장 을 불 러 올 수 있 는 대상 이다.
6. 서비스 층 서비스
1. @ Service 는 문맥 을 알려 주 고 서비스의 인 터 페 이 스 를 설명 합 니 다.
@Service
public interface CourseService {
Course getCourseById(IntegercouseId);
}
2. 이 인 터 페 이 스 를 실현 하 는 서비스 클래스 를 작성 하고 spring 에 게 서비스 클래스 라 고 알려 줍 니 다.
@Service("courseService")
public class CourseServiceImpl implements CourseService {
public Course getCourseById(Integer courseId)
{
Course course=new Course();
course.setCourseId(courseId);
course. setTitle ("Java 다 중 스 레 드");
course.setImgPath("resources/imgs/course-img.jpg");
course.setLearningNum(23568);
course.setLevel(2);
course. setLevelDesc ("중급");
course.setDuration(7200l);
course. setDescr ("다 중 스 레 드 는 일상 개발 에서 자주 사용 되 는 지식 이자 지식 을 사용 하기 어 려 우 므 로 반드시 잘 파악 해 야 합 니 다.")
ListchapterList=new ArrayList();
wrapChapterList(courseId,chapterList);
course.setChapterList(chapterList);
return course;
}
public void wrapChapterList(Integer courseId,ListchapterList)
{
Chapter chapter=new Chapter();
chapter.setId(1);
chapter.setCourseId(courseId);
chapter.setOrder(2);
chapter. setTitle ("제1장 Java 다 중 스 레 드 배경 응용");
chapter. setDescr ("자바 다 중 스 레 드 의 배경 응용 을 소개 하고 배경 지식 을 이해 하 며 해당 하 는 장면 을 잘 활용 할 수 있 습 니 다.")
chapterList.add(chapter);
Chapter chapter1=new Chapter();
chapter1.setId(1);
chapter1.setCourseId(courseId);
chapter1.setOrder(2);
chapter 1. setTitle ("제2 장 자바 스 레 드 초기 체험");
chapter 1. setDescr ("자바 언어 차원 에서 스 레 드 를 지원 합 니 다. 스 레 드 를 어떻게 만 들 고 시작 하 며 정지 합 니까? 자주 사용 하 는 스 레 드 방법 을 어떻게 사용 합 니까? 수나라 와 당나라 연 의 를 사례 로 설명 합 니 다.")
chapterList.add(chapter1);
}
}
참조 코드
Reference Demo: testMavenDemo