// 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);
動態方法調用:
第一種方法,指定action的mathom對應的方法調用
第二種方法:
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="login!hello.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;