spring mvc 之初级版

我也是最近刚开始接触spring mvc ,个人感觉比struts 方便多了。为了练手自己找了几个例子练手。
具体的操作步骤如下:
第一步:首先先导入MyEclipse 自带的spring 2.5 core和 spring 2.5 web 相关的 jar 包
第二步:配置applicationContext.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"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


  <bean id="loginController" class="com.pb.controller.LoginController">
   <property name="formView" value="login"></property>
   <property name="successView" value="showAccount"></property>
  </bean>
   <bean id="editController" class="com.pb.controller.EditController">
   <property name="successView" value="edit"></property>
  
  </bean>
  <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
     <props>
        <prop key="/login.do">loginController</prop>
        <prop key="/editBalance.do">editController</prop>
     </props>
    </property>
 </bean>
 <bean id="viewResolver"
   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/" />
      <property name="suffix" value=".jsp" />
 </bean>
</beans>
第三步:写一个Account实例第四步:编写处理业务逻辑的LoginController public class LoginController extends AbstractController {
    private String formView; //跳转的属性名
    private String successView;跳转的属性名
 @Override
 protected ModelAndView handleRequestInternal(HttpServletRequest request,
   HttpServletResponse response) throws Exception {
  
  //获取账号和密码
  String cardNo=request.getParameter("cardNo");
  String password=request.getParameter("password");
  //进行业务逻辑处理
  Account account=getAccount(cardNo, password);
  Map<String, Object> map=new HashMap<String, Object>();
  if(account!=null){
   map.put("account", account);
   //根据结果返回不同的ModelAnView
   return new ModelAndView(successView, map);--前台要获取的值都封装在map里
  }else{
   map.put("error","卡号或密码不正确");
   return new ModelAndView(formView,map);
  }
  
  
 }
 //获取账户和余额
   public Account getAccount(String cardNo,String password ){
    if(cardNo.equals("123")&& password.equals("123")){
     Account account=new Account();
     account.setCardNo(cardNo);
     account.setBalance(90.9f);
     return account;
    }else{
    return null; 
    }
   }
 public String getFormView() {
  return formView;
 }
 public void setFormView(String formView) {
  this.formView = formView;
 }
 public String getSuccessView() {
  return successView;
 }
 public void setSuccessView(String successView) {
  this.successView = successView;
 }
}

第五步:页面配置要想后台接受页面的数据,要保持和后台接受的参数名要和页面上的传递的账户名和密码的name名一致 <form action="login.do" method="post">
       <table>
         <tr><td colspan="2">账户登录</td></tr>
         <tr><td >帐户名:</td><td ><input type="text" name="cardNo" value=""/></td></tr>
         <tr><td >密码:</td><td ><input type="password" name="password" value=""/></td></tr>
         <tr><td colspan="2"><input type="submit"  value="提交"/><input type="reset"  value="重置"/></td></tr>
       </table>
    </form>
 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值