首先我将所有需要的文件引入:
loging.jsp
${error}
<body>
<form method="post" action="login.do">
<table>
<tr>
<td colspan="2" align="center">账户登录</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" align="center"><input type="submit" value="提交">
<input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
****************************************
shouwCount.jsp
<body>
账户信息:<br/>
卡号:${account.cardNo}<br/>
密码:${account.password}
</body>
******************************************
web.xml
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- servlet参数,此参数指定了Spring配置文件的位置,如果不配置,则默认为*/WEB-INF/<servlet name> , 其中<servlet name> 用Servlet名替换 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
********************************************
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">
<!-- 1.配置请求的controller的业务类 -->
<bean id="loginController" class="com.sp.web.controller.LoginController">
<property name="formView" value="login"></property><!-- 3.配置jsp作为试图解析器的操作使得 省去了".jsp" 字符-->
<property name="successView" value="showAccount"></property>
</bean>
<!-- 2.配置请controller 和 URL地址的映射关系 -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props><!-- 可以添加很多映射关系, -->
<prop key="/login.do">loginController</prop> <!-- 以“login.do的方法映射” -->
</props>
</property>
</bean>
<!-- 3.配置jsp作为试图解析器 -->
<!-- 返回什么样的试图呢?在web.xml文件中DispatcherServlet会找
viewResolver 这个视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"><!-- 给url地址加前缀 -->
</property>
<property name="suffix" value=".jsp"/><!-- 给url地址加后缀 -->
</bean>
</beans>
*****************************************
LoginController.java
package com.sp.web.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;//创建时所继承servlet
import org.springframework.web.servlet.mvc.AbstractController;
import com.sp.entity.Account;
public class LoginController extends AbstractController {
private String formView;//失败
private String successView;
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse reponse) throws Exception {
// TODO Auto-generated method stub
// 1.获取账户和密码
String cardNo = request.getParameter("cardNo");
String password = request.getParameter("password");
// 2.调用业务逻辑方法进行判断,在这里弱化业务逻辑的处理,直接在controller里实现业务逻辑的判断
Account account = getAccount(cardNo, password);
// 3.根据结果返回不同的ModelAndView对象
Map<String,Object> model = new HashMap<String, Object>();
if(account != null){
model.put("account", account);
return new ModelAndView(getSuccessView(),model);
}else{
model.put("error", "卡号和密码不正确");
return new ModelAndView(getFormView(),model);
}
}
public Account getAccount(String cardNo,String password){
if(cardNo.equals("123") && password.equals("123")){
Account account = new Account();
account.setCardNo("cardNo");
account.setBalance(0.3f);
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;
}
}
********************************
创建实体bean,Account.java
package com.sp.web.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;//创建时所继承servlet
import org.springframework.web.servlet.mvc.AbstractController;
import com.sp.entity.Account;
public class LoginController extends AbstractController {
private String formView;//失败
private String successView;
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse reponse) throws Exception {
// TODO Auto-generated method stub
// 1.获取账户和密码
String cardNo = request.getParameter("cardNo");
String password = request.getParameter("password");
// 2.调用业务逻辑方法进行判断,在这里弱化业务逻辑的处理,直接在controller里实现业务逻辑的判断
Account account = getAccount(cardNo, password);
// 3.根据结果返回不同的ModelAndView对象
Map<String,Object> model = new HashMap<String, Object>();
if(account != null){
model.put("account", account);
return new ModelAndView(getSuccessView(),model);
}else{
model.put("error", "卡号和密码不正确");
return new ModelAndView(getFormView(),model);
}
}
public Account getAccount(String cardNo,String password){
if(cardNo.equals("123") && password.equals("123")){
Account account = new Account();
account.setCardNo("cardNo");
account.setBalance(0.3f);
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;
}
}
以上是创建源文件,下面我来简单解析一下:
1。 由View层开始,看一下login.jsp,form内容会提交到login.do请求-->如何捕捉到login.do
我们需要由web.xml做起,
servlet-mapping配置会将以.do结尾的请求都交给DispatcherServlet来处理
<param-value>classpath:applicationContext.xml</param-value>
2. DispatcherServlet拿到这个请求之后他会到applicationContext.xml去找处理的controller,即()
<prop key="/login.do">loginController</prop>
3. loginController经过一系列的业务处理后会返回一个ModelAndView对象,
ModelAndView第一个对象参数是视图名称就是applicationContext注入的值
<property name="formView" value="login"></property>
4. 具体返回什么样的视图?DispatcherServlet会查找Viewresolve这个视图解析器,视图解析器找到了showAccount.jsp
shouAccount.jsp拿到model将后台得到的数据显示出来
5. model由loginController中的getAccount(cardNo, password);返回得到