我的RequestParam存在很大问题。我读了一些其他问题同样的问题,但我检查了语法一百次,我不知道我的代码有什么问题。我是春天的新人,如果有人能查看我的代码并帮我找出错误,那将是非常好的。
我有一个带有表单标记的jsp文件。在该形式中,用户可以选择他想要创建的图表类型。 我的jsp文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="piechart" method="get">
<input type="radio" name="pieChart" value="pie">pie<br>
<input type="radio" name="barChart" value="bar">bar<br>
<input type="submit" value="Submit">
</form>
<img src="/piechart" />
</body>
</html>
我的控制器看起来像这样:
package net.codejava.springmvc;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class ResultController {
private double value1 = 20.0;
private double value2 = 20.0;
private double value3 = 20.0;
private double value4 = 20.0;
private double value5 = 20.0;
private String answer1 = "very good: " + value1 + "%";
private String answer2 = "good: " + value2 + "%";
private String answer3 = "bad: " + value3 + "%";
private String answer4 = "very bad: " + value4 + "%";
private String answer5 ="no answer: " + value5 + "%";
@RequestMapping(value="/result", method= RequestMethod.GET)
public String test() {
return "jsp/result/pieChart";
}
@RequestMapping(value= "/piechart", method= RequestMethod.GET)
public void createChart(@RequestParam("pie") String pie, @RequestParam("bar") String bar, HttpServletResponse response) {
response.setContentType("image/png");
PieDataset pieDataset = createDataset();
JFreeChart pieChart = createChart(pieDataset, "Question");
try {
ChartUtilities.writeChartAsPNG(response.getOutputStream(), pieChart, 750, 400);
} catch(IOException ex){
ex.printStackTrace();
}
}
/** * set Data*/
public PieDataset createDataset() {
DefaultPieDataset result = new DefaultPieDataset();
result.setValue(answer1, 20);
result.setValue(answer2, 20);
result.setValue(answer3, 20);
result.setValue(answer4, 20);
result.setValue(answer5, 20 );
return result;
}
/** * Creates a chart */
@SuppressWarnings("deprecation")
public JFreeChart createChart(PieDataset dataset, String title) {
JFreeChart chart = ChartFactory.createPieChart(title,
dataset,
true,
true,
false);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.9f);
plot.setLabelBackgroundPaint(Color.white);
plot.setBackgroundPaint(Color.LIGHT_GRAY);
plot.setLabelFont(new Font("SansSerif", Font.ITALIC, 12));
plot.setLabelLinksVisible(true);
plot.setBackgroundPaint(null);
plot.setInteriorGap(0.04);
plot.setOutlineVisible(true);
Color[] colors = {new Color(153,204,255), new Color(153,153,255), new Color(204,153,255), new Color(255,153,255), new Color(255,153,204)};
for (int i = 0; i < 5; i++) {
plot.setSectionPaint(i, colors[i]);
}
return chart;
}
}
目前只能创建一个饼图。我选择radiobutton后,@ RequestParam会显示我的饼图,我会很高兴。
谢谢:)
答案 0 :(得分:3)
您需要更改@RequestParam
中使用的名称,以反映您在表单中使用的名称。
public void createChart(@RequestParam(value ="pieChart", required = false) String pie, @RequestParam(value = "barChart", required = false) String bar, HttpServletResponse response)
由于浏览器不允许选择两个无线电,我们需要向两者添加required = false
。
在createChart
内,你需要确保至少有一个参数被填充