spring mvc 1.0

4584 ワード

1.まずpom.xmlファイルを作成し、Spring MVCに依存するjarパッケージを追加します.
2.web.xmlを編集し、Spring MVCの先端コントローラを追加します.コードは以下の通りです
<servlet>
  	<servlet-name>bbs</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
 	<servlet-name>bbs</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
3.私たちのservlet-nameはbsであると宣言していますので、web-infoの下にbs-servlet.xmlファイルを作成したいです.
まず次のコードを追加します.
<context:component-scan base-package=「comp.neu.edu.bs.web」/>
このようにspringはcomp.neu.edu.bs.ウェブのディレクトリの下でControllerを検索します.
次のコードを追加します.
<bean id="viewResolver"
	      
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
          <property name="prefix">
              <value>/WEB-INF/jsp/</value>
           </property>
          <property name="suffix">
             <value>.jsp</value>
          </property>
</bean>
このコードはView Resoloverを構成しています.具体的な機能は後にします.
今はspringの配置がほぼ完了しました.簡単な登録例を書きます.
1.signIn.jspを作成する
<form:form method="POST" commandName="account">
		<table>
			<tr>
				<td>username:</td>
				<td><form:input path="username"/></td>
			</tr>
			<tr>
				<td>password</td>
				<td><form:password path="password"/></td>
			</tr>
			<tr>
				<td>
					<input type="submit" value="submit" />
				</td>
			</tr>
		</table>
</form:form>
主な機能はバックグラウンドのように二つのパラメータを提出し、それぞれusernameとpasswordです.
上記のコードを見たら疑問があるかもしれませんが、なぜ上のフォームにactionがないですか?このjspはControllerから回転してくるので、具体的な流れはControllerコードに会います.
2.以下は主なControllerコードで、AcceountController:
package com.neu.edu.bbs.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import com.neu.edu.bbs.model.Account;

@Controller
@RequestMapping("/account")
public class AccountController {
	private static final String REGISTER_URL = "signIn";
	private static final String REGISTER_SUCCESS = "signSuccess";
	
	@RequestMapping(method = RequestMethod.GET)
	public String initForm(ModelMap model) {
		Account account = new Account();
		model.addAttribute("account", account);
		return REGISTER_URL;
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String addAccount(@ModelAttribute ("account") Account account,
			BindingResult result, SessionStatus status) {
		System.out.println(account.getUsername() + account.getPassword());
		return "redirect:/account/" + account.getUsername();
	}
	
	@RequestMapping(value="{accountName}")
	public String getAccount(@PathVariable("accountName") String accountName,
			ModelMap model) {
		Account account = new Account();
		account.setUsername(accountName);
		model.addAttribute("account", account);
		return REGISTER_SUCCESS;
	}
}
流れの説明:
(1)まずブラウザに下記の住所を入力します.http://localhost:8080/sixuebbs/account
これは私たちのController類のRequest Mappingの構成によって、initForm方法が呼び出されます.initFormメソッドの機能は、Acceountオブジェクトを作成し、sighIn.jspページ、つまり上のページにジャンプします.
(2)signIn.jspページに登録情報を記入して提出し、addAcceountという方法を呼び出して、Acceountの内容をデータベースに実例化します.実装が成功したら方法はredirect/account/account Nameになります.このようにgetAcceountは実行されます.getAcceountという方法のRequest Mappingを見ます.これはRESTの味を含んでいます.
(3)最後にgetAccentが登録成功インターフェースに遷移します.登録成功画面に歓迎メッセージが表示されます.
なぜ私たちは二歩目に成功インターフェースにジャンプしないですか?もし私たちがこのようにすれば、私たちが再度成功インターフェースを更新すると、データは繰り返し提出されるからです.