'The NetBeans E-commerce Tutorial'에 해당되는 글 1

  1. 2011.01.23 [The NetBeans E-commerce Tutorial] 5. Preparing the Page Views and Controller Servlet

[The NetBeans E-commerce Tutorial] 5. Preparing the Page Views and Controller Servlet

[todo] 이거에 대한 화면 캡쳐를 보여주면 이해가 잘될거 같다.

http://netbeans.org/kb/docs/javaee/ecommerce/page-views-controller.html

- index.jsp

<body>
    <div id="main">
        <div id="header">
            header
        </div>

        <div id="indexLeftColumn">
            left column
        </div>

        <div id="indexRightColumn">
            right column
        </div>

        <div id="footer">
            footer
        </div>
    </div>
</body>
affablebean.css
body {
    font-family: Arial, Helvetica, sans-serif;
    width: 850px;
    text-align: center;
    margin: 20px auto;
}

#main { background: #eee }

#header {
    height: 250px;
    background: #aaa;
}

#footer {
    height: 60px;
    clear: left;
    background: #aaa;
}

#indexLeftColumn {
    height: 400px;
    width: 350px;
    float: left;
    background: #ccc;
}

#indexRightColumn {
    height: 400px;
    width: 500px;
    float: left;
    background: #eee;
}

  • width와 height로 위치를 잡고 background로 공간이 보이게 한다.
  • margin: 20px auto
    위, 아래로 20px을 띄우고, auto로 좌, 우로도 그만큼 띄운다.
  • float: left
    좌, 우측 컬럼을 만든다.
  • clear: left
    footer에 넣어서 위쪽 border가 표시되게 한다.
    (이게 없으면 footer가 제대로 보이지 않는다.)

- div 로 페이지 구조 잡기
  1. Create the structure in HTML.
  2. Create a set of styles to define the appearance.
  3. View the page to examine the results of your changes.

- jsp 파일을 WEB-INF/view/ 로 옮김.

- Creating a Header and Footer
include를 해도 되지만 모든 페이지에서 반복해야 한다.
header와 footer를 지정하는 방법이 있다.
web.xml 에 추가
    <jsp-config>
        <jsp-property-group>
            <description>header and footer settings</description>
            <url-pattern>/index.jsp</url-pattern>
            <url-pattern>/WEB-INF/view/*</url-pattern>
            <include-prelude>/WEB-INF/jspf/header.jspf</include-prelude>
            <include-coda>/WEB-INF/jspf/footer.jspf</include-coda>
        </jsp-property-group>
    </jsp-config>

- Controller Servlet 작성
@WebServlet(name = "Controller",
            loadOnStartup = 1,
            urlPatterns = {"/category", "/addToCart", "/viewCart", "/updateCart", "/checkout", "/purchase", "/chooseLanguage"}
)
public class ControllerServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

        String userPath = request.getServletPath();
        if (userPath.equals("/category")) {
		} else if (userPath.equals("/viewCart")) {
            userPath = "/cart";
        } else if (userPath.equals("/checkout")) {
        } else if (userPath.equals("/chooseLanguage")) {
        }
        String url = "/WEB-INF/view" + userPath + ".jsp";
        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String userPath = request.getServletPath();
        if (userPath.equals("/addToCart")) {
        } else if (userPath.equals("/updateCart")) {
        } else if (userPath.equals("/purchase")) {
            userPath = "/confirmation";
        }
        String url = "/WEB-INF/view" + userPath + ".jsp";
        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}