2013-02-10 75 views
0

我是Spring的新手,我正在開發虛擬Bank交易項目。現在,我創建了一個歡迎頁面,將我鏈接到可以進行存款或取款交易的頁面。數據庫和一切工作正常。而且,我有一張表格,顯示具有4個屬性(id,name,acctNo,balance)的客戶列表。該ID鏈接到下一頁,我只想顯示有關此客戶的信息。我怎樣才能做到這一點。Spring MVC-如何解析視圖?

的調度-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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" 
    xmlns:p="http://www.springframework.org/schema/p">  
    <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> 
    <bean id="urlMapping" 
     class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property name="interceptors"> 
      <list> 
       <ref local="localeChangeInterceptor"/> 
      </list> 
     </property> 
     <property name="urlMap"> 
      <map>    
       <entry key="/login.html"> 
        <ref bean="userloginController"/> 
       </entry> 
      </map>   
     </property>    
    </bean> 


    <!-- I tried adding this bean but noe luck, not sure where to use this id to map this bean --> 
    <bean id="showindividualCustomer" class="com.showCustomerController">s 
     <property name="successView"> <value>ViewCustomer</value></property> 
    </bean> 

    <bean id="userloginController" class="com.UserLoginFormController"> 
     <property name="sessionForm"><value>false</value></property> 
     <property name="commandName"><value>userLogin</value></property> 
     <property name="commandClass"><value>com.UserLogin</value></property> 
     <property name="formView"><value>userLogin</value></property> 
     <property name="successView"><value>showCustomer</value></property> 
    </bean> 

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
     <property name="paramName" value="hl"/> 
    </bean> 
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>  
</beans> 

而對於豆控制器= showindividualcustomer是:

public class showCustomerController extends SimpleFormController{ 

    protected ModelAndView onSubmit(Object obj) throws ServletException{ 

     return new ModelAndView("ViewCustomer");//name of the jsp page inside WEB-INF/jsp 
    } 
} 

謝謝!

回答

2

我實際上建議充分利用spring 3,看看你的代碼,我假設你正在看spring 2教程。

使用Spring 2.5.6或彈簧3 *你可以只是做:

@RequestMapping(value="/customer/{id}") 
    public String showCustomerInformation(@PathVariable String id, HttpServletRequest request){ 
     //your logic to get the customer information and pass it on 
     return "customerInformation"; 
    } 

一些參考: http://tech-read.com/2011/10/31/spring-3-mvc-annotations/ http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

編輯: 你的方法簽名更改爲:

onSubmit(HttpServletRequest request, Object command) throws ServletException 

只需使用:

request.getParameter("id") 
+0

謝謝您的建議,但您能否爲我提供Spring 2.x中的解決方案 – 2013-02-10 22:58:49

+0

已針對spring 2.0進行了更新 – 2013-02-11 05:12:19

相關問題