ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 55일 차 Spring MVC 어노테이션 ( 실전에 제일 가까움 ) 2번째
    삼성SDS_멀티캠퍼스/Spring Frame Work 2015. 11. 25. 14:22
    반응형

    Spring MVC 어노테이션을 이용해서 로그인을 할 수 있도록 만들어보자.









    시나리오



    1. SpringMVC02_Ex 프로젝트 생성 ( 메이븐 프로젝트로 변경 )


    2. 에노테이션 기반 컨트롤러 작성


    3. loginForm.do 요청이 들어오면 loginForm.jsp로 이동


    4. login.do 요청이 오면 id와 pw 파라미터를 비교해서 서로 같으면 loginSucc.jsp로 이동


    5. 아니면 loginFail.jsp로 이동







    <pom.xml>




    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>SpringMVC02_Ex</groupId>

    <artifactId>SpringMVC02_Ex</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <properties>

    <spring_version>3.2.8.RELEASE</spring_version>

    <mysql_version>5.1.35</mysql_version>

    <commons_version>1.4</commons_version>

    <aspectj_version>1.7.4</aspectj_version>

    <spring-webmvc>3.2.8.RELEASE</spring-webmvc>

    <spring-web>3.2.8.RELEASE</spring-web>

    </properties>

    <packaging>war</packaging>

    <build>

    <sourceDirectory>src</sourceDirectory>

    <plugins>

    <plugin>

    <artifactId>maven-war-plugin</artifactId>

    <version>2.6</version>

    <configuration>

    <warSourceDirectory>WebContent</warSourceDirectory>

    <failOnMissingWebXml>false</failOnMissingWebXml>

    </configuration>

    </plugin>

    <plugin>

    <artifactId>maven-compiler-plugin</artifactId>

    <version>3.3</version>

    <configuration>

    <source>1.7</source>

    <target>1.7</target>

    </configuration>

    </plugin>

    </plugins>

    </build>



    <dependencies>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-core</artifactId>

    <version>${spring_version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>${spring_version}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>${spring-webmvc}</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-web</artifactId>

    <version>${spring-web}</version>

    </dependency>




    </dependencies>




    </project>




    *작업*


    Spring core / context / webmvc / web을


    http://www.mvnrepository.com/


    이곳에 들어가서 위의 4가지를 모두 적용시키셔도 됩니다





    <web.xml>



    <?xml version="1.0" encoding="UTF-8"?>

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

    id="WebApp_ID" version="3.0">

    <display-name>SpringMVC02_Ex</display-name>

    <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

    </welcome-file-list>



    <servlet>

    <servlet-name>dispatcher</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <load-on-startup>1</load-on-startup>

    </servlet>


    <servlet-mapping>

    <servlet-name>dispatcher</servlet-name>

    <url-pattern>*.do</url-pattern>

    </servlet-mapping>



    </web-app>











    *작업*


    dispatcher servlet클래스를 임포트하고 url패턴을 지정한다.







    <dispatcher-servlet.xml>




    <?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"

    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-3.2.xsd">


    <context:component-scan base-package="controller"></context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="suffix" value=".jsp"></property>

    <property name="prefix" value="jsp/"></property>

    </bean>


    </beans>








    *작업*


    suffix는 접두이고

    prefix는 접미입니다.


    InternalResourceViewResolver클래스는


    컨트롤러의 ModelAndView 객체에서 setViewName메소드를 사용할 때


    WebContent/prefix/xxxx(suffix) 이런식으로 적용되기 때문에


    WebContent에서 따로 디렉토리를 만들지 않고 할 경우는 prefix를 지정하지 않아도 됩니다


    하지만 그렇게 할 경우 프로젝트의 규모가 방대하여 jsp파일 갯수가 증가하면


    관리측면에서 굉장히 불편하기 때문에


    Default 패키지인 WebContent의 하위 디렉토리를 따로 생성하여 관리하는 것이 좋습니다.




    이번에는 




    이렇게 하였기 때문에


    suffix와 prefix를 지정하였습니다.








    <LoginController.java>




    package controller;


    import org.springframework.stereotype.Controller;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.servlet.ModelAndView;


    @Controller

    public class LoginController {

    @RequestMapping("loginForm.do")

    public ModelAndView loginForm() {


    ModelAndView modelAndView = new ModelAndView();

    modelAndView.setViewName("loginForm");

    return modelAndView;

    }


    @RequestMapping("login.do")

    public ModelAndView login(String id, String pw) {


    ModelAndView modelAndView;


    if (id.equals("wooki") && pw.equals("123"))

    // if (id.equals(pw)) 

    {

    modelAndView = new ModelAndView();

    modelAndView.addObject("id", id);

    modelAndView.setViewName("loginSucc");

    } else {

    modelAndView = new ModelAndView();

    modelAndView.addObject("fail", "로그인에 실패하였습니다");

    modelAndView.setViewName("loginFail");

    }

    return modelAndView;


    }

    }






    *작업*

    기존에 하던 것처럼

    만들면 됩니다.





    <loginForm.jsp>




    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Insert title here</title>

    </head>

    <body>


    <form action="login.do">

    아이디<input type="text"  name="id"> <br> 

    비밀번호<input type="text" name="pw"> <br>

    <input type="submit" value="확인">

    </form>

    </body>

    </html>




    *작업*


    앞으로 만들 jsp들이 그렇겠지만



    jsp들은 이제부터 View의 역할만 합니다.



    계산하는 역할은 컨트롤러에 맡기고 View의 역할만 하도록 만듭니다.





    <loginFail.jsp>



    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Insert title here</title>

    </head>

    <body>

    ${fail }

    </body>

    </html>





    <loginSucc.jsp>




    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Insert title here</title>

    </head>

    <body>

    ${id }님 환영합니다

    </body>

    </html>








    < 결과 >






    반응형
Designed by Tistory.