spring mvc 如何通过ACTION访问在WEB-INF下的html
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring mvc 如何通过ACTION访问在WEB-INF下的html相关的知识,希望对你有一定的参考价值。
页面展示是用的html5,文件存放在WEB-INF下,现在想通过spring mvc的Controller来访问
通过试图解析器 以下是代码请供参考!
@RequestMapping("h_handelList")public ModelAndView h_handelList () throws Exception
ModelAndView mv = new ModelAndView();
mv.setViewName("/WEB-INF/jsp/h_handelList.html");
return mv;
也可以在springMVC.xml里配置试图解析器的前缀和后缀
<!-- 配置springmvc的视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".html"/>
</bean>
配置了前缀后缀后可以这样写:
@RequestMapping("h_handelList")public ModelAndView h_handelList () throws Exception
ModelAndView mv = new ModelAndView();
mv.setViewName("h_handelList");
return mv;
试图解析器是必须要配置的 可以不配置前缀后缀!
配置ViewResolver即可,可以配多个的
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".html"/>
</bean>
spring mvc jfreechar注解配置及数据库访问
1.action类,使用注解配置的:
package com.t5.manage.action;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.t5.entity.Statistics;
import com.t5.entity.User;
import com.t5.manage.biz.ManagerLoginBiz;
import com.t5.manage.biz.ManagerOrderBiz;
@Controller
public class Jfreechart {
@Autowired
public ManagerOrderBiz managerOrderBiz;
@Resource
public ManagerLoginBiz manageUserService;
@RequestMapping(value = "/getMajorChart")
public ModelAndView getMajorChart(HttpServletRequest request,
HttpServletResponse response, ModelMap modelMap,String time) throws Exception{
CategoryDataset dataset = getDataSet2(time);//time参数
JFreeChart chart = ChartFactory.createBarChart3D("全年销售报表", // 图表标题
"类别", // 目录轴的显示标签
"销售额", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
//解决乱码问题
getChartByFont(chart);
String fileName = ServletUtilities.saveChartAsJPEG(chart, 700,500, null, request.getSession());
String chartURL=request.getContextPath() + "/chart?filename="+fileName;
modelMap.put("chartURL", chartURL);
return new ModelAndView("test",modelMap);
}
private void getChartByFont(JFreeChart chart) {
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体",Font.BOLD,20));
LegendTitle legend = chart.getLegend();
if (legend!=null) {
legend.setItemFont(new Font("宋体", Font.BOLD, 20));
}
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis axis = plot.getDomainAxis();
//设置X轴坐标上标题的文字
axis.setLabelFont(new Font("宋体",Font.BOLD,22));
//设置X轴坐标上的文字,
axis.setTickLabelFont(new Font("宋体",Font.BOLD,12));
ValueAxis valueAxis = plot.getRangeAxis();
//设置Y轴坐标上标题的文字
valueAxis.setLabelFont(new Font("宋体",Font.BOLD,12));
//设置Y轴坐标上的文字
valueAxis.setTickLabelFont(new Font("sans-serif",Font.BOLD,12));
}
/**
* 获取一个演示用的组合数据集对象
*
* @return
* time 是查询参数
*/
private CategoryDataset getDataSet2(String time) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
List<Map<String, Object>> list=managerOrderBiz.getOrderList(time);
List<Statistics> liststa=new ArrayList<Statistics>();
System.out.println("集合数"+list.size());
for (Map<String, Object> map : list)
{
String typeName=(String) map.get("typeName").toString();//商品类型
String year=(String) map.get("year").toString(); //年
String month=(String) map.get("month").toString(); //月
Integer shoppingId=Integer.valueOf( map.get("shoppingId").toString());
Integer userId=Integer.valueOf( map.get("userId").toString());
Integer orderTypeId=Integer.valueOf((String)map.get("orderTypeId").toString());
double moneyTotal=Double.valueOf( map.get("moneyTotal").toString());
Statistics sta=new Statistics();
sta.setTypeName(typeName);
sta.setYear(year);
sta.setMonth(month);
sta.setMoneyTotal(moneyTotal);//总金额
liststa.add(sta);
}
for(Statistics tics:liststa){
dataset.addValue(tics.getMoneyTotal(), tics.getTypeName(),tics.getMonth());
}
return dataset;
}
}
2.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>jfreechart 演示 </title>
</head>
<body>
jfreechart 演示 <br>
请访问<a href="getMajorChart.do">fffff</a>
<br>
<img src="${chartURL}">
</body>
</html>
以上是关于spring mvc 如何通过ACTION访问在WEB-INF下的html的主要内容,如果未能解决你的问题,请参考以下文章
spring3 mvc 中怎么从当前action跳到另一个action中
asp.net mvc项目部署以后通过域名访问,IIS好像找不到默认文档
spring mvc jfreechar注解配置及数据库访问
请问在.net mvc3.0中,如何在action之间传递参数,切记不能在地址栏中看到
SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-006- 如何保持重定向的request数据(用model占位符RedirectAttributes)(代码