图书馆借了本SSH的书,有点儿简单的速成的感觉,虽然每个框架都需要学习很长时间才能深入,但这样学习也不错,有个整体上的了解后,再在实践中一步步完善,从而有深入的认识
今天看的是Spring 的MVC框架,实例是简单的用户登陆,功能是对用户登陆进行用户名和密码的判断,及登陆成功失败后的跳转.废话不多说,直接开始
开发环境是Myeclipse 7,tomcat 6,spirng2.0(做小程序,版本基本没影响吧)
需要的包主要是Spring Core,Spring MVC两个,但MyEclipse自动导入了很多工具包,如log4j等,其中一些是否必须使用我还没有验证
1.视图层view:3个视图页
index.jsp 首页,实现登陆表单的提交
success.jsp 成功后的跳转页面,显示欢迎
failure.jsp 失败后的跳转页面,显示失败原因
代码:
index.jsp
主要就是个表单
1
<form action="login.asp">
2
userName:<input type="text" name="username" /><br/>
3
password:<input type="password" name="password"/><br/>
4
<input type="submit" value="提交"/>
5
</form>
注意:action的值是login.asp,纯属无聊,因为可以配置过滤的格式,所以这里写什么结尾是无所谓的,只是想试下效果而已,
success.jsp
登陆成功,欢迎<%=request.getAttribute("username")%>
显示成功
failure.jsp
登陆失败,<%=request.getAttribute("msg")%>
显示失败
2.模型层model:模拟实现数据库的功能,及用户验证的方法
UserInfoBean.java
1 package springmvc.model;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import com.sun.org.apache.bcel.internal.generic.NEW;
6
7 public class UserInfoBean {
8 public static Map<String, String> map = new HashMap<String, String>();
9 //模拟数据库
10 static{
11 String personOneUsername ="xiaosan";
12 String personOnePasswrod = "123";
13 String personTwousename = "asi";
14 String personTwoPassword = "456";
15 map.put(personOneUsername, personOnePasswrod);
16 map.put(personTwousename, personTwoPassword);
17 }
18 //判断用户是否存在
19 public static boolean exisitUser(String username){
20 return map.containsKey(username);
21 }
22 //判断一个已存在的用户密码是否正确
23 public static boolean confirmPassword(String username,String password){
24 return map.get(username).equals(password);
25
26 }
27 }
28
3.控制层controller:调用模型层方法来实现页面跳转
LoginController.java 需要实现Controller接口
1
package springmvc.controller;
2
3
import java.util.HashMap;
4
import java.util.Map;
5
6
import javax.servlet.http.HttpServletRequest;
7
import javax.servlet.http.HttpServletResponse;
8
9
import org.springframework.web.servlet.ModelAndView;
10
import org.springframework.web.servlet.mvc.Controller;
11
12
import springmvc.model.UserInfoBean;
13
14
public class LoginController implements Controller
{
15
16
private String successPage ;
17
private String failurePage ;
18
@Override
19
public ModelAndView handleRequest(HttpServletRequest req,
20
HttpServletResponse resp) throws Exception
{
21
String username = req.getParameter("username");
22
String password = req.getParameter("password");
23
String message = null;
24
25
//判断是否为空
26
if(username == null||password ==null||username.equals("")||password.equals(""))
{
27
message = "username or password can't be null";
28
Map<String,String> model = new HashMap<String,String>();
29
model.put("msg", message);
30
return new ModelAndView(getFailurePage(),model);
31
}
32
//判断用户名是否存在
33
if(!UserInfoBean.exisitUser(username))
{
34
message ="username doesn't exisit";
35
Map<String,String> model = new HashMap<String,String>();
36
model.put("msg", message);
37
return new ModelAndView(getFailurePage(),model);
38
}
39
//如果存在,判断密码是否正确,正确跳转,错误跳转
40
if(!UserInfoBean.confirmPassword(username, password))
{
41
message ="password is wrong";
42
Map<String,String> model = new HashMap<String,String>();
43
model.put("msg", message);
44
return new ModelAndView(getFailurePage(),model);
45
}else
{
46
Map<String,String> model = new HashMap<String,String>();
47
model.put("username", username);
48
return new ModelAndView(getSuccessPage(),model);
49
}
50
}
51
//getter and setter
52
public String getSuccessPage()
{
53
return successPage;
54
}
55
public void setSuccessPage(String successPage)
{
56
this.successPage = successPage;
57
}
58
public String getFailurePage()
{
59
return failurePage;
60
}
61
public void setFailurePage(String failurePage)
{
62
this.failurePage = failurePage;
63
}
64
65
}
66
4.最后是两个配置文件:web.xml和spring配置文件,applicationContext.xml
web.xml
<!-- 配置spring的后台servlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定spring配置文件的路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 拦截所有以.asp结尾的请求,可以修改 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.asp</url-pattern>
</servlet-mapping>
从这个配置文件可以知道Spring的配置文件名及位置是可以更改的.
ApplictionContext.xml
<bean id="urlMappint" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- login.asp由id为login的bean处理 -->
<prop key="login.asp">login</prop>
</props>
</property>
</bean>
<!-- 配置控制器 -->
<bean id="login" class="springmvc.controller.LoginController">
<!-- 注入失败页面属性 -->
<property name="failurePage">
<value>failure.jsp</value>
</property>
<!-- 注入成功页面属性 -->
<property name="successPage">
<value>success.jsp</value>
</property>
</bean>
这个配置文件配置了请求的处理方向和控制器两个属性的注入
运行就可以了,结果就是控制器里的结果,这样,就完成了这个简单实例.
现在对SpringMVC有了个初步的了解,由于接触Spring才几天,所以问题还有很多,如
Spring的注入方式,虽然都看过了,但是还没怎么使用,这个例子中只涉及了set值注入,还是比较简单的,没什么理解上的难度,对于其他几种方式,等都理解了再做个总结吧,
由于springFramwork这个单词很长,所以他的类路径就显的特别长,加上还不太熟悉他的结构,所以哪里需要用到那些类还是不太明白,先遇到一个记一个吧,这里用到的就有配置文件中的
org.springframework.web.servlet.DispatcherServlet、控制url处理的org.springframework.web.servlet.handler.SimpleUrlHandlerMapping等类.
SpringMVC中,控制器要返回一个ModelAndView,这个类的构造函数太多了,具体都是传什么值,什么作用,还没有深入了解.
SpringMVC就暂时到这里了,看了网上对它的评价,虽然最近用到肯能性不大,但对Spring的学习,还是起了一定的作用的.
源文件下载,没有LIB,需要自己导
突然明白,崩溃的日子,是自己的不努力造成的.