Stripes学习一: 一个小实例
- 下载stripes.jar包,并把它放进/WEB/lib目录下
- 复制StripesResources.properties到/WEB/classes目录下
- 配置
使用Stripes首先在web.xml中要做以下配置
1
<
filter
>
2 < display-name > Stripes Filter </ display-name >
3 < filter-name > StripesFilter </ filter-name >
4 < filter-class > net.sourceforge.stripes.controller.StripesFilter </ filter-class >
5 </ filter >
6
7 < filter-mapping >
8 < filter-name > StripesFilter </ filter-name >
9 < url-pattern > *.jsp </ url-pattern >
10 < dispatcher > REQUEST </ dispatcher >
11 </ filter-mapping >
12
13 < filter-mapping >
14 < filter-name > StripesFilter </ filter-name >
15 < servlet-name > StripesDispatcher </ servlet-name >
16 < dispatcher > REQUEST </ dispatcher >
17 </ filter-mapping >
18
19 < servlet >
20 < servlet-name > StripesDispatcher </ servlet-name >
21 < servlet-class > net.sourceforge.stripes.controller.DispatcherServlet </ servlet-class >
22 < load-on-startup > 1 </ load-on-startup >
23 </ servlet >
24
25 < servlet-mapping >
26 < servlet-name > StripesDispatcher </ servlet-name >
27 < url-pattern > *.action </ url-pattern >
28 </ servlet-mapping >
29
2 < display-name > Stripes Filter </ display-name >
3 < filter-name > StripesFilter </ filter-name >
4 < filter-class > net.sourceforge.stripes.controller.StripesFilter </ filter-class >
5 </ filter >
6
7 < filter-mapping >
8 < filter-name > StripesFilter </ filter-name >
9 < url-pattern > *.jsp </ url-pattern >
10 < dispatcher > REQUEST </ dispatcher >
11 </ filter-mapping >
12
13 < filter-mapping >
14 < filter-name > StripesFilter </ filter-name >
15 < servlet-name > StripesDispatcher </ servlet-name >
16 < dispatcher > REQUEST </ dispatcher >
17 </ filter-mapping >
18
19 < servlet >
20 < servlet-name > StripesDispatcher </ servlet-name >
21 < servlet-class > net.sourceforge.stripes.controller.DispatcherServlet </ servlet-class >
22 < load-on-startup > 1 </ load-on-startup >
23 </ servlet >
24
25 < servlet-mapping >
26 < servlet-name > StripesDispatcher </ servlet-name >
27 < url-pattern > *.action </ url-pattern >
28 </ servlet-mapping >
29
4、写前台jsp页面
<%
@ page contentType="text/html;charset=UTF-8" language="java"
%>
<% @ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
< html >
< head >< title > My First Stripe </ title ></ head >
< body >
< stripes:form action ="/test.action" focus ="" >
< table >
< tr >
< td > 名字: </ td >
< td >< stripes:text name ="name" /></ td >
</ tr >
< tr >
< td > 密码: </ td >
< td >< stripes:text name ="password" /></ td >
</ tr >
< tr >
< td colspan ="2" >
< stripes:submit name ="execute" value ="提交" />
</ td >
</ tr >
< tr >
< td > 结果: </ td >
< td > ${actionBean.result} </ td >
</ tr >
</ table >
</ stripes:form >
</ body >
</ html >
<% @ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
< html >
< head >< title > My First Stripe </ title ></ head >
< body >
< stripes:form action ="/test.action" focus ="" >
< table >
< tr >
< td > 名字: </ td >
< td >< stripes:text name ="name" /></ td >
</ tr >
< tr >
< td > 密码: </ td >
< td >< stripes:text name ="password" /></ td >
</ tr >
< tr >
< td colspan ="2" >
< stripes:submit name ="execute" value ="提交" />
</ td >
</ tr >
< tr >
< td > 结果: </ td >
< td > ${actionBean.result} </ td >
</ tr >
</ table >
</ stripes:form >
</ body >
</ html >
5、后台
1
package
com.test.action;
2
3 import net.sourceforge.stripes.action.ActionBean;
4 import net.sourceforge.stripes.action.ActionBeanContext;
5 import net.sourceforge.stripes.action.DefaultHandler;
6 import net.sourceforge.stripes.action.ForwardResolution;
7 import net.sourceforge.stripes.action.Resolution;
8
9 public class Test implements ActionBean {
10 private String name;
11 private String password;
12 private String result;
13 @DefaultHandler
14 public Resolution execute() {
15 result="用户名为:"+name+" 密码为:"+password;
16 return new ForwardResolution("/test1.jsp");
17 }
18 public String getResult() {
19 return result;
20}
21 private ActionBeanContext context;
22
23 public void setPassword(String password) {
24 this.password = password;
25}
26 public void setName(String name) {
27 this.name = name;
28}
29
30 public ActionBeanContext getContext() {
31
32 return context;
33 }
34
35 public void setContext(ActionBeanContext context) {
36 this.context=context;
37
38 }
39
40}
41
2
3 import net.sourceforge.stripes.action.ActionBean;
4 import net.sourceforge.stripes.action.ActionBeanContext;
5 import net.sourceforge.stripes.action.DefaultHandler;
6 import net.sourceforge.stripes.action.ForwardResolution;
7 import net.sourceforge.stripes.action.Resolution;
8
9 public class Test implements ActionBean {
10 private String name;
11 private String password;
12 private String result;
13 @DefaultHandler
14 public Resolution execute() {
15 result="用户名为:"+name+" 密码为:"+password;
16 return new ForwardResolution("/test1.jsp");
17 }
18 public String getResult() {
19 return result;
20}
21 private ActionBeanContext context;
22
23 public void setPassword(String password) {
24 this.password = password;
25}
26 public void setName(String name) {
27 this.name = name;
28}
29
30 public ActionBeanContext getContext() {
31
32 return context;
33 }
34
35 public void setContext(ActionBeanContext context) {
36 this.context=context;
37
38 }
39
40}
41
6、 在Tomcat中访问http://localhost:8080/Test.action即可