Changes between Initial Version and Version 1 of waue/2011/0907


Ignore:
Timestamp:
Sep 7, 2011, 5:43:44 PM (13 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2011/0907

    v1 v1  
     1{{{
     2#!html
     3<div style="text-align: center; color:#151B8D"><big style="font-weight: bold;"><big><big>
     4Spring Web MVC Eclipse 編譯環境
     5</big></big></big></div> <div style="text-align: center; color:#7E2217"><big style="font-weight: bold;"><big>
     6Spring 3.1 + Eclipse 3.6EE + Tomcat 6 + WTP 3.2.4
     7</big></big></div>
     8}}}
     9[[PageOutline]]
     10
     11
     12= 安裝環境 =
     13
     14 * Spring 3.1
     15 * Eclipse 3.6EE
     16 * Tomcat 6
     17下載 tomcat 專案到系統內
     18 * WTP 3.2.4
     19在 Eclipse->Help->"install new software" 安裝 WTP 3.2.4 與 WTP 3.2.4 SDK
     20
     21= Eclipse =
     22
     23 * 改寫 http://caterpillar.onlyfun.net/Gossip/SpringGossip/FirstSpringMVC.html,但按照此網頁程式跑不出來,因此而外補充之。
     24
     25== 新增專案 ==
     26new project -> other -> "web" : "Dynamic Web Project" -> project name = "Web1SpringMVC" -> 完成
     27
     28== 匯入Web App Libraties ==
     29
     30加入所有spring*.jar 到 WebContent/WEB-INF/lib/ 內
     31
     32用 指令 或用 file->import->"file system" 精靈 將 spring/dist/*.jar 匯入到此資料夾
     33
     34== 匯入Java Resources ==
     35
     36在 java build path -> libraries : add external jars ->  選擇 spring/dist/*.jar  -> ok
     37
     38== 程式碼 ==
     39
     40 * WebContent/WEB-INF/jsp/hello.jsp
     41
     42{{{
     43#!java
     44<%@page contentType="text/html"%>
     45<%@page pageEncoding="UTF-8"%>
     46
     47<!doctype html>
     48<html>
     49    <head>
     50        <meta charset="UTF-8">
     51        <title>First Spring MVC</title>
     52    </head>
     53    <body>
     54        <h1>Hello, ${user}!!</h1>
     55        <p>usage: hello.do?user=xx</p>
     56    </body>
     57   
     58</html>
     59
     60}}}
     61
     62 * WebContent/WEB-INF/web.xml
     63
     64{{{
     65#!xml
     66
     67<?xml version="1.0" encoding="UTF-8"?>
     68<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee →" version="2.4">
     69  <session-config>
     70    <session-timeout>30</session-timeout>
     71  </session-config>
     72  <servlet>
     73    <servlet-name>DispatcherServlet</servlet-name>
     74    <servlet-class>
     75            org.springframework.web.servlet.DispatcherServlet
     76        </servlet-class>
     77    <init-param>
     78      <javaee:param-name>contextConfigLocation</javaee:param-name>
     79      <javaee:param-value>/WEB-INF/mvc-config.xml</javaee:param-value>
     80    </init-param>
     81    <load-on-startup>1</load-on-startup>
     82  </servlet>
     83  <servlet-mapping>
     84    <servlet-name>DispatcherServlet</servlet-name>
     85    <url-pattern>*.do</url-pattern>
     86  </servlet-mapping>
     87</web-app>
     88
     89}}}
     90
     91 * WebContent/WEB-INF/mvc-config.xml
     92
     93{{{
     94#!xml
     95<?xml version="1.0" encoding="UTF-8"?>
     96<!DOCTYPE beans PUBLIC
     97 "-//SPRING/DTD BEAN/EN"
     98 "http://www.springframework.org/dtd/spring-beans.dtd">
     99
     100<beans>   
     101    <bean id="viewResolver"
     102          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     103        <property name="prefix">
     104            <value>/WEB-INF/jsp/</value>
     105        </property>
     106        <property name="suffix">
     107            <value>.jsp</value>
     108        </property>
     109    </bean>
     110   
     111    <bean name="/hello.do"
     112          class="org.waue.HelloController">
     113        <property name="viewPage">
     114            <value>hello</value>
     115        </property>
     116    </bean>
     117</beans>
     118}}}
     119
     120
     121 * "Java Resource"-> src/org.waue/HelloController.java
     122
     123{{{
     124package org.waue;
     125
     126import javax.servlet.http.HttpServletRequest;
     127import javax.servlet.http.HttpServletResponse;
     128
     129import org.springframework.web.servlet.ModelAndView;
     130import org.springframework.web.servlet.mvc.Controller;
     131
     132public class HelloController implements Controller {
     133    private String viewPage;
     134
     135    public ModelAndView handleRequest(HttpServletRequest req,
     136                                            HttpServletResponse res)
     137                                                     throws Exception {
     138        String user = req.getParameter("user");
     139        return new ModelAndView(viewPage, "user", user);
     140    }
     141   
     142    public void setViewPage(String viewPage) {
     143        this.viewPage = viewPage;
     144    }
     145}
     146}}}
     147
     148
     149= 結果 =
     150
     151
     152
     153
     154