struts2.0(60-70)

// private HttpServletRequest request;

// request.setAttribute("zhangsan","helloworld");

    // public void setServletRequest(HttpServletRequest request)

    // {

    // System.out.println("request");

    // this.request = request;

    // }

 

}

Action中:

public class LoginAction extends ActionSupport implements ServletResponseAware

{

    // private HttpServletResponse response;

public String execute() throws Exception

    {         

           Cookie cookie = new Cookie("username",this.getUsername());//设置cookie

          

           cookie.setMaxAge(1000);

          

           response.addCookie(cookie);

          

           return "success";

       }

    }

    // public void setServletResponse (HttpServletResponse response)

    // {

    // System.out.println("response ");

    // this. response = response;

    // }

 

}

页面获取:cookie: ${cookie.username.value }

 

第三种办法:servletActionContext

HttpServletResponse response = ServletActionContext.getResponse();

          

           Cookie cookie = new Cookie("username",this.getUsername());

          

           cookie.setMaxAge(1000);

          

           response.addCookie(cookie);

 

动态方法调用:

第一种方法,指定actionmathom对应的方法调用

第二种方法:

Action例如有下列方法:

   

    @SuppressWarnings("unchecked")

    public String hello () throws Exception

    {

      

       System.out.println("login invoked");

      

       if ("hello".equals(this.getUsername().trim())

              && "world".equals(this.getPassword().trim()))

       {

           Map map = ActionContext.getContext().getSession();

          

           map.put("user","valid");

          

          

           // ActionContext.getContext().put("zhangsan","helloworld");

          

           // request.setAttribute("zhangsan","helloworld");

          

           HttpServletResponse response = ServletActionContext.getResponse();

          

           Cookie cookie = new Cookie("username",this.getUsername());

          

           cookie.setMaxAge(1000);

          

           response.addCookie(cookie);

          

           return "success";

       }

       else

       {

           this.addFieldError("username", "username or password error");

           return "failer";

       }

 

    }

页面修改:

<s:form action="loginhello.action">//第一个login对象action里面的name而第二个hello匹配对应的方法

Struts.xml不做修改

    <action name="Login" class="com.test.action.LoginAction">

通配符的使用,不建议

Struts.xml配置

    <action name="*Login" class="com.test.action.LoginAction" method="{1}">//1代表第一个型号

页面修改:<s:form action="helloLogin">

处理结果:type的设置一些东西

<action name="*Login" class="com.test.action.LoginAction" method="{1}">

           <result name="input">/login2.jsp</result>

           <result name="success" type="httpheader">

              <param name="status">500</param>

           </result>

           <result name="failer">/login2.jsp</result>

       </action>

声明式异常

页面:

<s:form action="login">

 

<s:textfield name="username" label="username"></s:textfield>

<s:password name="password" label="password"></s:password>

 

<s:submit label="submit"></s:submit>

 

</s:form>

Struts.xml:先找局部才找全局异常

//全局异常,注意顺序

<global-results>

           <result name="login" type="redirect">/login2.jsp</result>

           <result name="passwordInvalid">/passwordInvalid.jsp</result>

       </global-results>

      

      

       <global-exception-mappings>

           <exception-mapping result="passwordInvalid" exception="com.test.exception.PasswordException"></exception-mapping>

       </global-exception-mappings>

//局部异常

        <action name="login" class="com.test.action.LoginAction">

           <exception-mapping result="usernameInvalid" exception="com.test.exception.UsernameException"></exception-mapping>//异常类和返回页面的定义

           <result name="success">/result.jsp</result>

           <result name="usernameInvalid">/usernameInvalid.jsp</result>

       </action>

Action

public class LoginAction extends ActionSupport

{

    private String username;

    private String password;

---提供getset方法----

    @SuppressWarnings("unchecked")

    public String execute() throws Exception

    {

       //username invalid

       if(!"hello".equals(this.getUsername()))

       {

           throw new UsernameException("username invalid");

       }

       //password invalid

       else if(!"world".equals(this.getPassword()))

       {

           throw new PasswordException("password invalid");

       }

       else

       {

           return SUCCESS;

       }

    }

}

 

 

 

异常类的定义:

package com.test.exception;

 

public class UsernameException extends Exception

{

    private String message;

   

    public UsernameException(String message)

    {

       super(message);

      

       this.message = message;

    }

 

    public String getMessage()

    {

       return message;

    }

 

    public void setMessage(String message)

    {

       this.message = message;

    }

   

   

}

package com.test.exception;

 

public class PasswordException extends Exception

{

    private String message;

 

    public PasswordException(String message)

    {

       super(message);

 

       this.message = message;

    }

 

    public String getMessage()

    {

       return message;

    }

 

    public void setMessage(String message)

    {

       this.message = message;

    }

 

}

 

异常捕获页面:

<%@ page language="java" contentType="text/html; charset=GB18030"

    pageEncoding="GB18030"%>

   

<%@ taglib prefix="s" uri="/struts-tags" %>

   

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=GB18030">

<title>Insert title here</title>

</head>

<body>

${exception.message }

${exception.message.message }

<s:property value="exceptionStack"/>

</body>

</html>

Jfreechar的使用:http://www.jfree.org/

加载jar包:

jcommon-1.0.16.jar

jfreechart-1.0.13.jar

 

生成文档:cmd进入目录:C:/Documents and Settings/Administrator/桌面/向电脑桌面/Incoming/struts-2.1.8.1-all/jfreechart-1.0.13/jfreechart-1.0.13/ant

再运行 ant javadoc就生成相应的文档了。

饼图:

public class JFreeChartTest

{

    public static void main(String[] args)

    {

       //默认的数据集,不同类型的数据集是不同的

       DefaultPieDataset dpd = new DefaultPieDataset();

 

       dpd.setValue("管理人员", 25);

       dpd.setValue("市场人员", 25);

       dpd.setValue("开发人员", 45);

       dpd.setValue("其他人员", 10);

        //建立chart实例,图标本身

       JFreeChart chart = ChartFactory.createPieChart3D("某公司人员组织结构图", dpd, true,

              true, false);

 

       ChartFrame chartFrame = new ChartFrame("某公司人员组织结构图", chart);

 

       chartFrame.pack();

 

       chartFrame.setVisible(true);

 

    }

}

 

柱状图:

package com.test.jfreechart;

 

import java.awt.Font;

 

import javax.swing.JPanel;

 

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.CategoryAxis;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.chart.title.TextTitle;

import org.jfree.data.category.CategoryDataset;

import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.ui.ApplicationFrame;

 

public class JFreeChartTest2 extends ApplicationFrame

{

    //定义构造方法实现applicationFrame

    public JFreeChartTest2(String title)

    {

       super(title);

 

       this.setContentPane(createPanel());

    }

    //准备数据集

    public static CategoryDataset createDataset()

    {

       //定义柱状图的数据集

       DefaultCategoryDataset dataset = new DefaultCategoryDataset();

 

       dataset.setValue(10, "aa", "管理人员");

       dataset.setValue(20, "bb", "市场人员");

       dataset.setValue(40, "cc", "开发人员");

       dataset.setValue(15, "dd", "其他人员");

 

       return dataset;

    }

    //定义图表本身

    public static JFreeChart createChart(CategoryDataset dataset)

    {

       JFreeChart chart = ChartFactory.createBarChart3D("hello", "人员分布", "人员数量",

              dataset, PlotOrientation.VERTICAL, true, false, false);

        //更改标题

       chart.setTitle(new TextTitle("某公司组织结构图", new Font("宋体", Font.BOLD

              + Font.ITALIC, 20)));

        //中间部分plot

       CategoryPlot plot = (CategoryPlot) chart.getPlot();

        //获得横坐标

       CategoryAxis categoryAxis = plot.getDomainAxis();

        //设置字体

       categoryAxis.setLabelFont(new Font("微软雅黑", Font.BOLD, 12));

 

       return chart;

 

    }

    //定义面板

    public static JPanel createPanel()

    {

       JFreeChart chart = createChart(createDataset());

 

       return new ChartPanel(chart);

    }

 

    public static void main(String[] args)

    {

       //生成实例

       JFreeChartTest2 chart = new JFreeChartTest2("某公司组织结构图");

 

       chart.pack();//swing里面的方法

       chart.setVisible(true);

    }

 

}

 

生成图片:

public class JFreeChartTest3

{

    public static void main(String[] args) throws Exception

    {

       JFreeChart chart = ChartFactory.createPieChart("某公司组织结构图",

              getDataset(), true, false, false);

 

       chart

              .setTitle(new TextTitle("某公司组织结构图", new Font("宋体", Font.BOLD,

                     22)));

        //获得图例,第一图例(0

       LegendTitle legend = chart.getLegend(0);

 

       legend.setItemFont(new Font("微软雅黑", Font.BOLD, 14));

        //中间图例

       PiePlot plot = (PiePlot) chart.getPlot();

 

       plot.setLabelFont(new Font("隶书", Font.BOLD, 16));

        //输出流

       OutputStream os = new FileOutputStream("company.jpeg");

        //写到流里面

       ChartUtilities.writeChartAsJPEG(os, chart, 1000, 800);

 

       os.close();

 

    }

 

    private static DefaultPieDataset getDataset()

    {

       DefaultPieDataset dpd = new DefaultPieDataset();

 

       dpd.setValue("管理人员", 25);

       dpd.setValue("市场人员", 25);

       dpd.setValue("开发人员", 45);

       dpd.setValue("其他人员", 10);

 

       return dpd;

    }

 

}

Jsp页面显示图片:

web.xml里面加入下面代码

    <servlet>

       <servlet-name>DisplayChart</servlet-name>

       <servlet-class>

           org.jfree.chart.servlet.DisplayChart

       </servlet-class>

    </servlet>

    <servlet-mapping>

       <servlet-name>DisplayChart</servlet-name>

       <url-pattern>/DisplayChart</url-pattern>

    </servlet-mapping>

2.建立jsp文件

<%@ page language="java" contentType="text/html; charset=GB18030"

    pageEncoding="GB18030"%>

   

<%@ page import="org.jfree.data.general.DefaultPieDataset,org.jfree.chart.ChartFactory

,org.jfree.chart.JFreeChart,org.jfree.chart.servlet.*" %>

   

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=GB18030">

<title>Insert title here</title>

</head>

<body>

 

<%

//准备数据集

DefaultPieDataset dpd = new DefaultPieDataset();

 

dpd.setValue("管理人员", 25);

dpd.setValue("市场人员", 25);

dpd.setValue("开发人员", 45);

dpd.setValue("其他人员", 10);

//由数据集,工厂类构造图表

JFreeChart chart = ChartFactory.createPieChart3D("某公司组织结构图",dpd, true, false, false);

//保存图像文件,保存在服务器的临时目录下

String fileName = ServletUtilities.saveChartAsPNG(chart,800,600,session);

//filename是固定的

String url = request.getContextPath() + "/DisplayChart?filename=" + fileName;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值