struts2笔记,从零搭建一个web项目
1、新建项目:包结构,需要导入的包,jdk版本为1.7
2、web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!-- struts2的核心拦截器 :本质:就是实现Filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 45分钟 --> <session-config> <session-timeout>45</session-timeout> </session-config> <!-- 根目录进入的欢迎页面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> |
3、struts.xml
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> </struts> |
4、测试:写一个测试类
1 2 3 4 5 6 |
public class TestAction { public String execute(){//这是默认进入的方法 System.out.println("进来了吗"); return "success"; } } |
再去struts.xml注册该类
1 2 3 4 |
<!-- 测试action代码 ,默认进入的方法不需要指明--> <action name="test" class="com.tz.action.TestAction"> <result name="success">/index.jsp</result> </action> |
测试,看能不能正常访问test,有问题,解决了再往下。
5、引入页面html,js,css,images,访问一个页面看是否有错。
6、在entity包中建立实体类,定义属性,生成对应的get和set方法。
7、在dao包中建立增删改查的业务类,测试查询能不能正常查询出结果。
8、为了把查询的数据返回到页面中去,我们需要定义一个action类,为其指定返回的页面。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//定义一个全局的profitBeans用于接收查询结果,记得一定要生成get和set方法, //相当于request.setAttribute("profitBeans",profitBeans); private List<ProfitBean> profitBeans; public String profit(){ System.out.println("进来了吗"); profitBeans = ProfitDao.findProfits(); return "success"; } public List<ProfitBean> getProfitBeans() { return profitBeans; } public void setProfitBeans(List<ProfitBean> profitBeans) { this.profitBeans = profitBeans; } |
同时还要在struts.xml文件中注册对应的类和方法。
1 2 3 4 5 |
<action name="profit" method="profit" class="com.tz.action.ProfitAction"> <result name="success">/index.jsp</result> <!-- 引入登录拦截器 --> <interceptor-ref name="loginStrack"></interceptor-ref> </action> |
9、为了在页面能对接收到的返回的数据进行解析,需要在页面中引入标签。
1 |
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> |
然后在页面中使用返回的数据。
1 2 3 4 5 6 7 8 |
<c:forEach var="profit" items="${profitBeans}"> <ul> <li>${profit.id}</li> <li>${profit.money}</li> <li>${profit.status}</li> <li>${profit.isDelete}</li> </ul> </c:forEach> |
也可以引入自定义的标签。
1 |
<%@taglib uri="/WEB-INF/tld/tz.tld" prefix="tz"%> |
10、关于删除和编辑等功能,使用ajax实现,需要在struts.xml中注册ajax。
1 2 3 4 5 6 7 8 9 10 |
<package name="jsonstruts2" namespace="/json" extends="json-default"> <action name="delete/*" method="delete" class="com.tz.action.ProfitAction"> <param name="id">{1}</param> <result type="json" name="ajaxSuccess"> <param name="ignoreHierarchy">false</param><!-- 这个属性表示是否忽略等级,也就是继承关系,设置为 false 后会将父类和子类的属性一起返回 --> <param name="excludeNullProperties">true</param><!-- 这个属性表示去除空数据 --> </result> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> |
java业务方法。
1 2 3 4 5 6 7 8 9 |
public String delete(){ boolean flag = ProfitDao.updateProfit(29,1); if(flag){ result = "success"; }else{ result = "fail"; } return "ajaxSuccess"; } |
js交互。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$.ajax({ type:"post", url:basePath+"/json/delete/"+opid, success:function(data){ if(data.result=="success"){ $(obj).parents(".tmui-items").fadeOut("slow",function(){ $(this).remove(); }); }else{ alert("删除失败!"); } } }); |
11、关于全局页面跳转和拦截器的注册
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!-- 注册拦截器 --> <interceptors> <!-- 登陆拦截器 --> <interceptor name="loginInterceptor" class="com.tz.core.LoginInterceptor"></interceptor> <interceptor-stack name="loginStrack"> <interceptor-ref name="loginInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 全局页面跳转 --> <global-results> <!-- 登陆页面跳转,访问页面的跳转,这个服务于http://localhost:8080/report/login--> <result name="login">/login.jsp</result> <!-- 登陆action跳转 --> <result name="loginRedirect" type="redirect">/login</result> </global-results> |
注意点:package里元素必须按照一定的顺序排列。这个顺序就是
result-types
interceptors
default-interceptor-ref
default-action-ref
default-class-ref
global-results
global-exception-mappings
action*(就是所有的action放到最后)
12、自定义标签。
先在一个类中定义好方法:
1 2 3 4 5 6 7 8 9 10 11 |
public class TmFunctions { /** * 扩展字符串判断的indexOf方法 * 方法名:indexOf<BR> * 创建人:veznlee <BR> */ public static int indexOf(String content,String searchStr){ if(StringUtils.isEmpty(content))return -1; return content.indexOf(searchStr) ; } } |
在一个tld(tz.tld)文件中注册该方法的使用,在web-info下新建一个文件,放到里面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <!-- 自定义标签的描述 --> <description>自定义标签tz</description> <display-name>自定一标签-函数形式</display-name> <tlib-version>1.0</tlib-version> <!-- 自定义标签的前缀 --> <short-name>tz</short-name> <!-- 自定义标签的uri如果你采用的jstl1.2以上的版本 --> <uri></uri> <!-- 函数标签 --> <function> <!-- 标签的描述 --> <description>非空判断</description> <!-- 标签名称 :命名规则:简单易懂,方便记忆 --> <name>indexOf</name> <!-- 标签对应的类 --> <function-class>com.tz.util.TmFunctions</function-class> <!-- 类中对应的方法 --> <function-signature>int indexOf(java.lang.String ,java.lang.String)</function-signature> <!-- 例子 --> <example>${tz:indexOf("ccc",'a')}==-1</example> </function> </taglib> |
需要使用的页面中引入:
1 |
<%@taglib uri="/WEB-INF/tld/tz.tld" prefix="tz"%> |
13、标签的使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<!--c:forEach--> <select name="profitBean.typeId" id="typeId" class="text_box"> <option value="">--请选择收入类型--</option> <c:forEach var="pt" items="${maps}"> <option ${pt.id==profitBean.typeId ? 'selected="selected"':''} value="${pt.id}">${pt.name}</option> </c:forEach> </select> <!--c:choose--> <c:choose> <c:when test="${tz:indexOf(pageContext.request.requestURI,'profit/list')==-1}"> <li><a href="${basePath}/profit/list">收入明细</a></li> </c:when> <c:otherwise> <li class="selected"><a href="javascript:void(0);">收入明细</a></li> </c:otherwise> </c:choose> <!--自定义标签--> <c:forEach var="profit" items="${profitBeans}"> <tr class="tmui-items"> <td>${profit.id}</td> <td>¥${profit.money}</td> <td class="key">${profit.typeName}</td> <td class="key">${profit.username}</td> <!--自定义标签--> <td>${tz:formateDate(profit.createTime,'yyyy-MM-dd HH:mm:ss')}</td> <td>${profit.description}</td> <td><a href="${basePath}/profit/add/${profit.id}" data-opid="${profit.id}">编辑</a> <a href="javascript:void(0)" onclick="tm_delete(this)" data-opid="${profit.id}">删除</a></td> </tr> </c:forEach> |
发表评论