进入 Spring MVC (2)
配置应用程序的 URL
下一步是配置想让 sampleBankingServlet 处理的 URL。同样,还是要在 web.xml 中注册所有这些信息。
清单 2. 配置想要处理的 URL
sampleBankingServlet
*.jsp
装入配置文件
下面,装入配置文件。为了做到这点,请为 Servlet 2.3 规范注册 ContextLoaderListener 或为 Servlet 2.2 及以下的容器注册 ContextLoaderServlet。为了保障后向兼容性,请用 ContextLoaderServlet。在启动 Web 应用程序时,ContextLoaderServlet 会装入 Spring 配置文件。清单 3 注册了 ContextLoaderServlet。
清单 3. 注册 ContextLoaderServlet
context>servlet-name>
org.springframework.web.context.ContextLoaderServlet
字串6
1
contextConfigLocation 参数定义了要装入的 Spring 配置文件,如下面的 servlet 上下文所示。
contextConfigLocation/WEB-INF/sampleBanking-services.xml
sampleBanking-services.xml 文件代表示例银行应用程序服务的配置和 bean 配置。如果想装入多个配置文件,可以在 标记中用逗号作分隔符。
Spring MVC 示例
示例银行应用程序允许用户根据惟一的 ID 和口令查看帐户信息。虽然 Spring MVC 提供了其他选项,但是我将采用 JSP 技术作为视图页面。这个简单的应用程序包含一个视图页用于用户输入(ID 和口令),另一页显示用户的帐户信息。
我从 LoginBankController 开始,它扩展了 Spring MVC 的 SimpleFormController。SimpleFormContoller 提供了显示从 HTTP GET 请求接收到的表单的功能,以及处理从 HTTP POST 接收到的相同表单数据的功能。LoginBankController 用 AuthenticationService 和 AccountServices 服务进行验证,并执行帐户活动。“ 配置视图属性 ”一节中的 清单 5 描述了如何把 AuthenticationService 和 AccountServices 连接到 LoginBankController。 清单 4 显示了 LoginBankController 的代码。
字串1
清单 4. LoginBankController 扩展 SimpleFormController
public class LoginBankController extends SimpleFormController {
public LoginBankController(){
}
protected ModelAndView onSubmit(Object command) throws Exception{
LoginCommand loginCommand = (LoginCommand) command;
authenticationService.authenticate(loginCommand);
AccountDetail accountdetail = accountServices.getAccountSummary(loginCommand.getUserId());
return new ModelAndView(getSuccessView(),"accountdetail",accountdetail);
}
private AuthenticationService authenticationService;
private AccountServices accountServices;
public AccountServices getAccountServices() {
字串5
return accountServices;
}
public void setAccountServices(AccountServices accountServices) {
this.accountServices = accountServices;
}
public AuthenticationService getAuthenticationService() {
return authenticationService;
}
public void setAuthenticationService(
AuthenticationService authenticationService) {
this.authenticationService = authenticationService;
}
}
配置视图属性
下面,我必须注册在接收到 HTTP GET 请求时显示的页面。我在 Spring 配置中用 formView 属性注册这个页面,如清单 5 所示。sucessView 属性代表表单数据提交而且 doSubmitAction() 方法中的逻辑成功执行之后显示的页面。formView 和 sucessView 属性都代表被定义的视图的逻辑名称,逻辑名称映射到实际的视图页面。 字串5
清单 5. 注册 LoginBankController
class="springexample.controller.LoginBankController">
true
loginCommand
springexample.commands.LoginCommand
login
accountdetail
commandClass 和 commandName 标记决定将在视图页面中活动的 bean。例如,可以通过 login.jsp 页面访问 loginCommand bean,这个页面是应用程序的登录页面。一旦用户提交了登录页面,应用程序就可以从 LoginBankController 的 onSubmit() 方法中的命令对象检索出表单数据。
字串4
Tags:
责任编辑:
上一篇:进入 Spring MVC (3) 下一篇:进入 Spring MVC (1)
您的评论
·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为
精彩推荐
最新资讯


您的位置: