Struts2学习日志2
来源:互联网 发布:win10不能下载软件 编辑:程序博客网 时间:2024/06/07 08:58
Action的方法访问:
1使用action标签中的 method 属性为这个name 指定一个方法(这个应该不用解释了)
2使用通配符的方法:
3.使用动态方法实现(不常用 而且麻烦 感兴趣自行百度)
为应用指定多个配置文件:
在<struts>中 加入<includefile=""/> 一般以模块来分开
配置全局性的result 在某个包下创建<global-result>节点再在其中建result节点 其中去定义一些属性
如果想要其他包也能访问到 可以继承该包,当然该包继承的是struts-defualt. 别的包继承了这个包 当然也继承了struts-defualt.和java的继承关系差不多.
常量配置:
将struts.devMode的value设置为true。从而当更改struts.xml文件后不需要重新启动服务就可以进行程序调试
<constantname="struts.devMode" value="true" />
<constantname="struts.i18n.encoding" value="UTF-8" />
当然还有很多常用的常量还有很多具体可以去百度下. 第二种配置常量的方法是在struts.properties 文件下去配置 感兴趣自行百度 但是不怎常用,因为第一种就够了。
Demo:
1.目录结构:
2.代码(代码中将所说到的都使用了):
1 struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true"></constant> <package name="default" extends="struts-default"> <global-results> <result name="error">/WEB-INF/public/error.jsp</result> </global-results> </package> <include file="student.xml"></include> </struts>
2 student.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="student" extends="default" namespace="/student"> <action name="Question_*" class="com.lsy.Action.QuestionAction" method="{1}"> <!--修改name属性 不用通配符 使用method="getList"也行 --> <result name="success">/WEB-INF/student/questionJsp.jsp</result> <!--因为默认是forward转发 所以从后台可以访问到WEB-INF中的jsp--> </action> </package> </struts>
3 QuestionAction.java
package com.lsy.Action;import java.util.ArrayList;import java.util.List;import com.lsy.bean.Question;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class QuestionAction extends ActionSupport {public String getList() throws Exception {List<Question> qlist= new ArrayList<Question>();for(int i=1;i<4;i++){Question q =new Question("第"+i+"个题目!!!!","简答题",i+"");qlist.add(q);if(i==4){//用于一会测试全局result的配置ActionContext.getContext().put("errmsg", "i=4所以跳到这个错误页面!!!");return "error";}}//往session域中添加试题集合 ActionContext.getContext().getSession().put("qlist",qlist);return this.SUCCESS;//SUCCESS为action接口里的常量,ActionSupport实现了action接口,这个又继承了这个ActionSupport类,故可以访问到}}
4 Question.java
package com.lsy.bean;public class Question {private String qname;private String type;private String answer;public Question(String qname, String type, String answer) {super();this.qname = qname;this.type = type;this.answer = answer;}public Question() {}public String getQname() {return qname;}public void setQname(String qname) {this.qname = qname;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getAnswer() {return answer;}public void setAnswer(String answer) {this.answer = answer;}}
5 jsp
<%@ page language="java" import="java.util.*" import="bean.*" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'questionJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> </head> <body><h3>试题信息!</h3><c:forEach items="${qlist}" var="question" varStatus="count">第${count.index}道题:<br>题目:${question.qname}<br>类型:${question.type}<br>答案:${question.answer}<br></c:forEach> </body></html>
总结:虽然用到的都是基础 但一切的累积都从最小的开始最后再附上测试结果
,error这个全局result就不测试了 测试只需要更改一下java代码其中的 i 的大小即可。
- Struts2学习日志2
- Struts2学习日志(2)初体验
- struts2 学习日志1
- struts2.1.6 学习日志一
- Redis学习日志【三、jedis+struts2】
- Struts2学习日志(3)编写Struts简单流程
- Struts2学习日志(4) validate验证数据
- 学习日志:精通struts2 Web2.0开发实战
- ognl和struts2标签_个人学习日志
- JAVA_WEB Struts2学习:Struts2标签学习2
- struts2学习笔记(2)
- struts2学习笔记2
- struts2-教程学习2
- Struts2学习笔记(2)
- struts2学习(2)
- struts2学习2
- Struts2学习笔记(2)
- Struts2学习-2
- shiro的一些事儿
- 简单工厂模式
- 华为云服务器无法ssh连接&&可以公网ip访问
- html 对 form 表单中 put,delete,patch的支持
- computer-database项目性能测试方案
- Struts2学习日志2
- java内部类
- 使用iframe实现将四个页面合成一个页面,并可以随意切换
- IFE_02_dynamicDataBinding_04
- 如何用好 github 中的 watch、star、fork
- idea 内存溢出配置
- Java面试宝典之--基本功
- 常用工具
- 网格变形动画