struts2.0(70-80)

来源:互联网 发布:常用网络命令 编辑:程序博客网 时间:2024/05/18 00:59

 

%>

 

<img src="<%= url %>" width="800" height="600">

 

 

</body>

</html>

 

投票jfreechart

页面:

<%@ 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>

 

<h1><font color="blue">请选择喜欢的运动项目</font></h1>

 

<s:form action="viewResult">

 

<s:checkbox name="interest" label="足球" fieldValue="football" labelposition="left"></s:checkbox>

<s:checkbox name="interest" label="篮球" fieldValue="basketball" labelposition="left"></s:checkbox>

<s:checkbox name="interest" label="排球" fieldValue="volleyball" labelposition="left"></s:checkbox>

<s:checkbox name="interest" label="羽毛球" fieldValue="badminton" labelposition="left"></s:checkbox>

 

<!--

<s:checkboxlist list="#{'computer' : '计算机' , 'math' :  '数学'}" name="interest" label="浪曦" labelposition="top">

</s:checkboxlist>

 -->

 

 

<s:submit value="提交"></s:submit>

 

</s:form>

 

 

</body>

</html>

 

 

Action

package com.test.action;

 

import java.awt.Font;

import java.util.List;

import java.util.Map;

 

import org.jfree.chart.ChartFactory;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.CategoryAxis;

import org.jfree.chart.axis.CategoryLabelPositions;

import org.jfree.chart.axis.NumberAxis;

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 com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

 

public class ViewResultAction extends ActionSupport

{

    private JFreeChart chart;//此定义是不能改变的

 

    private List<String> interest;

 

    public JFreeChart getChart()

    {

       chart = ChartFactory.createBarChart3D("兴趣统计结果", "项目", "结果", this

              .getDataset(), PlotOrientation.VERTICAL, false, false, false);

      

       chart.setTitle(new TextTitle("兴趣统计结果",new Font("黑体",Font.BOLD,22)));

      

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

      

       CategoryAxis categoryAxis = plot.getDomainAxis();

      

       categoryAxis.setLabelFont(new Font("宋体",Font.BOLD,22));

      

       categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//倾斜度

      

       NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();

      

       numberAxis.setLabelFont(new Font("宋体",Font.BOLD,22));

      

       return chart;

    }

 

    public List<String> getInterest()

    {

       return interest;

    }

 

    public void setInterest(List<String> interest)

    {

       this.interest = interest;

    }

 

    @Override

    public String execute() throws Exception

    {

       return SUCCESS;

    }

 

    @SuppressWarnings("unchecked")//应该放到model里面

    private void increaseResult(List<String> list)

    {

       ActionContext context = ActionContext.getContext();

 

       Map map = context.getApplication();

 

       for (String str : list)

       {

           if (null == map.get(str))

           {

              map.put(str, 1);

           }

           else

           {

              map.put(str, (Integer) map.get(str) + 1);

           }

       }

    }

 

    @SuppressWarnings("unchecked")

    private CategoryDataset getDataset()

    {

       DefaultCategoryDataset dataset = new DefaultCategoryDataset();

 

       this.increaseResult(this.getInterest());

 

       ActionContext context = ActionContext.getContext();

 

       Map map = context.getApplication();

 

       dataset.setValue((Integer) map.get("football"), "", "足球");

       dataset.setValue((Integer) map.get("basketball"), "", "篮球");

       dataset.setValue((Integer) map.get("volleyball"), "", "排球");

       dataset.setValue((Integer) map.get("badminton"), "", "羽毛球");

 

       return dataset;

    }

 

}

 

Struts.xml配置

解压:struts2-jfreechart-plugin-2.0.11.jar

修改struts-plugin.xml

<struts>

    <package name="jfreechart-default" extends="struts-default">

   

    <result-types>

        <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult">

            <param name="height">150</param>

            <param name="width">200</param>

        </result-type>

    </result-types>

    </package>

再重新打包:

Cmd ,转到struts-plugin.xml目录,jar就可以看到各种命令

使用如下命令就可以打包:

jar cvf struts2-jfreechart-plugin-2.0.11.jar (产生的文件名)-c *

改继承关系:

<package name="struts2" extends="jfreechart-default">

<action name="viewResult" class="com.test.action.ViewResultAction">

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

              <param name="height">600</param>

            <param name="width">800</param>

           </result>

       </action>

Struts2spring整合

加插件:struts2-spring-plugin-2.0.11.jar

选择MyEclipse的支持:spring2.0 core librarysweb选择copy checked library。。。。

 

建立接口

package com.test.service;

 

public interface LoginService

{

    public boolean isLogin(String username, String password);

}

 

实现:

package com.test.service.impl;

 

import com.test.service.LoginService;

 

public class LoginServiceImpl implements LoginService

{

    public boolean isLogin(String username, String password)

    {

       if ("hello".equals(username) && "world".equals(password))

       {

           return true;

       }

 

       return false;

    }

 

}

action中提供set方法:

public class LoginAction extends ActionSupport

{

    private String username;

    private String password;

 

    private LoginService loginService;

    public void setLoginService(LoginService loginService)

    {

       this.loginService = loginService;

    }

public String execute() throws Exception

    {

 

       if (loginService.isLogin(username, password))

       {

           return SUCCESS;

       }

       else

       {

           return INPUT;

       }

Struts.xml

//class 是自己起的名字

<action name="login" class="loginAction">

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

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

       </action>

 

Spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<beans

    xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

 

<bean id="loginService" class="com.test.service.impl.LoginServiceImpl"></bean>

//下面的ID要和struts中的自定义的class名称一样,让它来管理

<bean id="loginAction" class="com.test.action.LoginAction" scope="prototype">

    <property name="loginService">

       <ref local="loginService"/>

    </property>

</bean>

</beans>

配置 监听器:

Web.xml

    <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

同时把applicationContext.xml放到web-inf下或指定也行。

Struts2整合jasperReport报表整合 网址:www.jasperforge.org

辅助工具:iReport

水晶报表 Business object

Eclipse business intelligence and Reporting tool birt :www.eclipse.org/birt

 

iReport2.0.5建立报表和执行报表,链接数据库驱动

补充:将驱动放到它的lib目录下面,启动时就会自动加载进来

数据库工具:Toad for MySql

建立数据库

iReport2.0.5链接数据库

PTF

Itext用于Java转化ptf的格式,但不支持中文。下载itextasina.jar放到:C:/Documents and Settings/Administrator/桌面/向电脑桌面/Incoming/struts-2.1.8.1-all/iReport-3.7.0/ireport/modules/ext 文件下重新启动,修改字体属性,fort下的PDF font name stsong-light和改PDF encodingUCS2-H(Chinese Simplified)再把font name改成宋体

Struts2+hibernate3.2+spring2.0

 

前期工作:

先加hibernate支持:annotationscore。选择 copy checked….下面的选择都不选让spring进行管理

增加spring2.0支持,选择aopcore persistence corepersistence jdbc webliberaries支持或只加官网spring.jar即可

去到 aop Builder 去掉create spring sessionFactory

增加struts支持:freemarker.2.3.8.jar ognl2.6.11.jar struts2-core-2.0.11.jar xwork.2.0.4.jarstruts2-spring-plugin-2.0.11.jar

 

代码编写

src下建立struts.Xml

 

修改web.xml

 

 

    <filter>

       <filter-name>struts2</filter-name>

       <filter-class>

           org.apache.struts2.dispatcher.FilterDispatcher

       </filter-class>

    </filter>

 

    <filter-mapping>

       <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

 

    <welcome-file-list>

       <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

 

    <listener>

       <listener-class>

           org.springframework.web.context.ContextLoaderListener

       </listener-class>

    </listener>

 

 

连接池一保证有dbcp连接池 用命令进入MySQL :  MySQL –uroot –proot

可以用工具 toad for MySQL

修改工具里面的 Bast support for multilingualism 就是选择utf-8的编码方式

选择 include bin directory inwindows path//增加到path下面

 

建立页面:主页

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

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

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

   

    <title>operation</title>

  </head>

 

  <body>

   

    <h1><font color="red">Operation List</font></h1>

   

    <s:a href="save.jsp">Save User</s:a> <br><br><br>

   

    <s:a href="listUser.action">List Users</s:a>

   

   

  </body>

</html>

 

保存页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

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

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

   

    <title>Save User</title>

   

  </head>

 

  <body>

  

    <h1><font color="red">Save User</font></h1>

   

    <s:form action="saveUser">

    <s:textfield name="user.firstname" label="%{getText('firstname')}"></s:textfield>

上面的方式是取到资源文件内容

<s:textfield name="user.firstname" label="firstname></s:textfield>//静态文本形式

    <s:textfield name="user.lastname" label="%{getText('lastname')}"></s:textfield>

    <s:textfield name="user.age" label="%{getText('age')}"></s:textfield>

       <s:submit></s:submit>

          

    </s:form>

  

  </body>

</html>

 

建立模型驱动类也可以当实体类:

package com.test.bean;

 

public class User

{

    private Integer id;

    private String firstname;

    private String lastname;

    private int age;

 

设置getheset方法----

 

配置hbmxml

 

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping>

    <class name="com.test.bean.User" table="users">

       <id name="id" type="java.lang.Integer" column="id">

           <generator class="increment"></generator>

       </id>

 

       <property name="firstname" type="string" column="firstname"

           length="50">

       </property>

       <property name="lastname" type="string" column="lastname"

           length="50">

       </property>

       <property name="age" type="java.lang.Integer" column="age"></property>

 

    </class>

</hibernate-mapping>

 

建立action

package com.test.action.user;

 

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

 

import com.opensymphony.xwork2.ActionSupport;

import com.test.bean.User;