今天在网上看到有很多SpringMVC的例子,写的都不是很详细,于是找到官方的文档看了又看,鼓捣了一下午终于完成了一个小例子,由于本人也是刚开始接触SpringMVC,也有很多不懂的地方,如果写的不好,请大家不要骂。
1.首先把需要的类库导入,整个的结构大概是这样的:
在网上看了好多文章,都说把JSP放在WEB-INF下面是个不错的办法,防止有些淘气的人捣乱。
2.建立applicationContext.xml文件,代码如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/config/jdbc.properties" />
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}" />
当然这里没有用到数据源,可以不去管它。
3.接下来修改web.xml文件,代码如下
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
CMS
default.jsp
contextConfigLocation
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
Servlet定义这里我们定义了请求分发Servlet,即:org.springframework.web.servlet.DispatcherServlet
DispatcherServlet是Spring MVC中负责请求调度的核心引擎,所有的请求将由此Servlet根据配置分发至各个逻辑处理单元。其内部同时也维护了一个ApplicationContext实例。
-à
dispatcher
org.springframework.web.servlet.DispatcherServlet
2
请求映射我们将所有以.htm结尾的请求交给Spring MVC进行处理。当然,也可以设为其他值,如.action、.action等。当然用.htm的后缀是不推荐的。在tomcat上会出问题。
-à
dispatcher
*.htm
30
log4jConfigLocation
/WEB-INF/log4j.properties
4.然后创建dispatcher-servlet.xml内容如下:<?xmlversion ="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
indexController
testController
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
p:commandClass="com.zhkj.cms.controller.TestFrom"
p:fail_view="fail"
p:success_view="success" />
通过以上设定,Web服务器将把登录界面提交的请求转交给Dispatcher处理,Dispatcher将提取请求(HttpServletRequest)中的输入数据,分发给对应的处理单元,各单元处理完毕后,将输出页面返回给Web服务器,再由Web服务器返回给用户浏览器。Dispatcher根据什么分发这些请求?显然,我们还需要一个配置文件加以设定。这也就是上面提及的dispatcher-servlet.xml,此文件包含了所有的“请求/处理单元”关系映射设定,以及返回时表现层的一些属性设置。
5.接下来写一个pojo,也就是form类,名字为TestFrom。代码如下
publicclassTestFrom {
privateStringname;
privateStringpwd;
publicString getName() {
returnname;
}
publicvoidsetName(String name) {
this.name= name;
}
publicString getPwd() {
returnpwd;
}
publicvoidsetPwd(String pwd) {
this.pwd= pwd;
}
}
6.然后编写TestController.java代码如下:
importjava.util.HashMap;
importjava.util.LinkedList;
importjava.util.List;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.springframework.validation.BindException;
importorg.springframework.web.servlet.ModelAndView;
importorg.springframework.web.servlet.mvc.SimpleFormController;
publicclassTestControllerextendsSimpleFormController{
privateStringfail_view=null;
privateStringsuccess_view=null;
@Override
protectedModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throwsException {
TestFrom ts = (TestFrom)command;
if(ts.getName().equals("ly") && ts.getPwd().equals("123")){
HashMap map =newHashMap();
map.put("TestFrom", ts);
List msgList =newLinkedList();
msgList.add("这是什么?");
msgList.add("hello world");
msgList.add("你好,Spring MVC");
map.put("msg", msgList);
returnnewModelAndView(this.getSuccess_view(),map);
}else{
returnnewModelAndView(this.getFail_view());
}
}
publicString getFail_view() {
returnfail_view;
}
publicvoidsetFail_view(String fail_view) {
this.fail_view= fail_view;
}
publicString getSuccess_view() {
returnsuccess_view;
}
publicvoidsetSuccess_view(String success_view) {
this.success_view= success_view;
}
}
可以看到,类似Servlet的doGet/doPost方法,此版本的onSubmit方法签名中
包含了Servlet规范中的HttpServletRequest、HttpServletResponse以提
供与Web服务器的交互功能(如Session的访问)。此参数类型的onSubmit方法
的调用优先级较高。也就是说,如果我们在子类中同时覆盖了这两个不同参数的onSubmit方法,那么只有此版本的方法被执行,另一个将被忽略。
然后将输入的请求数据对象强制转型为预定义的请求对象类型。
返回处理结果
ModelAndView类包含了逻辑单元返回的结果数据集和表现层信息。ModelAndView
本身起到关系保存的作用。它将被传递给Dispatcher,由Dispatcher根据其中
保存的结果数据集和表现层设定合成最后的界面。
这里我们用到了两种签名版本的ModelAndView构造方法:
public ModelAndView(String viewname)
返回界面无需通过结果数据集进行填充。
public ModelAndView(String viewname, Map model)
返回界面由指定的结果数据集加以填充。可以看到,结果数据集采用了Map接口
实现的数据类型。其中包含了返回结果中的各个数据单元。关于结果数据集在界
面中的填充操作,可参见下面关于返回界面的描述。
上面这两个版本的构造子中,通过viewname指定了表示层资源。
另外,我们也可以通过传递View对象指定表示层资源。
public ModelAndView(View view)
public ModelAndView(View view, Map model)
我们可以结合RedirectView完成转向功能,如:
return new ModelAndView(
new RedirectView(“/redirected.jsp”
));
当然,我们也可以在带有HttpServletRequest参数的onSubmit方法实现中,通
过HttpServletRequest/HttpServletResponse完成forward/redirect功
能,这两种途径可以达到同样的效果。
7.接下来写index.jsp代码
pageEncoding="UTF-8"%>
Insert title here
name:
pwd:
8.编写fail.jsp这个页面是如果登录失败才会跳转到这里
pageEncoding="UTF-8"%>
Insert title here
登陆失败点此返回
9.编写success.jsp文件,如果登录成功,跳转到这个页面
pageEncoding="UTF-8"%>
Insert title here
登陆成功!!!!!!!!!!
${item}
如果各位按照上面的代码多试试,就会成功了。