3

一貫性と単純さのために、コントローラーの周りにセッションスコープのドメイン Bean を渡したいのですが、これは OOTB では不可能のようです。誰かがアドバイスできることを願っています。

質問:セッション スコープの Bean を MVC コントローラーの引数として公開できますか


これには注釈があるようです: @SessionAttributes("myBean") ただし、これはコントローラーレベルのスコープのみを維持します。

私は HttpSession とやり取りする必要がないようにし、代わりにドメイン オブジェクト グラフを一貫してコントローラーを介して渡したいと考えています。これはかなり標準的な要件のようです。

これには次の利点があります。

  • テスト容易性 - テストする Bean を注入するだけで、HttpSession をモックアウトする必要はありません
  • 抽象化 - サーブレット モデルの問題を回避し、ビジネス上の問題のみを回避

現在の構成は次のとおりです。

@Controller
@SessionAttributes("customer")
public class LoginController {

   @Inject Customer customer;

   @RequestMapping(value = "/login", method = RequestMethod.GET)
   public String welcome(Customer customer) {
         ...
         return "loginDetailsView";
   }

   public String processLogin(@Valid Customer customer, BindingResult bindingResult) {
         ...
         if (bindResult.hasErrors()) {
             return "loginDetailsView";
         else {
             return "homePageView";
         }
   }

「Customer」セッション Bean は、XML 構成の aop-proxy (CGLIB) を使用する通常の POJO であり、セッション スコープ Bean をシングルトン (コントローラーおよびサービス クラス) に注入できるようにします。

<beans:bean id="customer" class="com.mypackage.domain.Customer" scope="session">
            <aop:scoped-proxy proxy-target-class="true"/>
</beans:bean>

次の質問は実質的に同じですが、Spring 3 コア フレームワークへの拡張を伴わない回答はありません。

これは、Spring の設計者によって意図的に省略されていますか? 私の意図は、「フォーム Bean」とマッパーのレイヤーを使用せずに、ドメイン モデルを使用してフォームをサポートすることです。

誰でも方法をアドバイスできますか、またはこれが悪い習慣であるかどうかを示すことができますか?

オーダーメイドのソリューションに関する以前の質問:

Spring MVCでセッション属性をメソッド引数(パラメータ)として渡す方法

注意: Spring フレームワークへの依存を最小限に抑え、注釈サポート esp JSR-303 などを使用しようとしています。あらゆる OOTB ソリューションを採用します - カスタム拡張を提案しないでください。

4

1 に答える 1

0

Spring-MVC can expose the application context's beans to the view layer, if that is what you wish to do.

For example, the InternalResourceViewResolver can be instructed to expose every bean in the context, or just specified ones. Take a look at the exposeContextBeansAsAttributes and exposedContextBeanNames properties.

For example, say you wanted to expose the beans beanA and beanB to your JSPs. You would declare the view resolver in your context thus:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="exposedContextBeanNames">
      <list>
         <value>beanA</value>
         <value>beanB</value>
      </list>
   </property>
</bean>

Alternatively, to just expose every bean:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="exposeContextBeansAsAttributes" value="true"/>
</bean>
于 2012-11-08T06:11:12.293 に答える