struts2 集成 click framework

来源:互联网 发布:手机自动校时软件 编辑:程序博客网 时间:2024/05/22 01:41

1 web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <display-name>click</display-name>
 <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>*.action</url-pattern>
 </filter-mapping>
 
 <servlet>
  <servlet-name>click-servlet</servlet-name>
  <servlet-class>net.sf.click.ClickServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>click-servlet</servlet-name>
  <url-pattern>*.htm</url-pattern>
 </servlet-mapping>

 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>
</web-app> 

2 click.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<click-app charset="UTF-8">

 <pages package="demo.page" autobinding="true"></pages>
 <mode value="development" />

</click-app>

3 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

 <constant name="struts.enable.DynamicMethodInvocation" value="false" />
 <constant name="struts.devMode" value="false" />
 <!--
  <include file="example.xml"/>
  
  Add packages here -->
 <package name="struts2.action" extends="struts-default">
  <action name="HelloWorld" class="struts2.action.HelloWorld">
   <result>HelloWorld.jsp</result>
  </action>
  <!-- Add your actions here -->
 </package>


</struts>
4 click例子

package demo.page;

import net.sf.click.Page;
import net.sf.click.util.HtmlStringBuffer;

public class BorderPage extends Page {

 public String getTemplate() {
  return "border-template.htm";
 }
 

}

 

package demo.page;

import java.util.ArrayList;
import java.util.List;

import net.sf.click.Page;
import net.sf.click.control.Column;
import net.sf.click.control.Form;
import net.sf.click.control.Submit;
import net.sf.click.control.Table;
import net.sf.click.control.TextField;

public class HomePage extends BorderPage {
 public String title = "我的主页";
 public Table table = new Table();
 public Form form = new Form();
 public String msg;
 List<Tb> list = new ArrayList<Tb>();

 public HomePage() {
  table.setClass(Table.CLASS_SIMPLE);
  table.setId("myTABLE");
  table.addColumn(new Column("id"));
  table.addColumn(new Column("name"));
  table.addColumn(new Column("email"));
  table.addColumn(new Column("investments"));
  table.setPageSize(10);
  table.setShowBanner(true);
  table.setSortable(true);
  table.setShowBanner(true);

  form.add(new TextField("id", true));
  form.add(new TextField("email", true));
  form.add(new TextField("name", true));
  form.add(new TextField("investments", true));
  form.add(new Submit("提交"));

 }

 public boolean onSubmit() {
  if (form.isValid()) {
   msg = "Your name is " + form.getFieldValue("name");
  }
  return true;
 }

 public boolean onOkClicked() {
  if (form.isValid()) {
   Tb tb = new Tb();
   form.copyTo(tb);
   tb.setId((long) (this.list.size() + 1));
   this.list.add(tb);
   table.setRowList(list);
   form.clearValues();
   msg = "A new customer record has been created.";
  }
  return true;
 }

 public void onRender() {
  for (int i = 0; i < 200; i++) {
   Tb tb = new Tb();
   tb.setEmail("email" + i);
   tb.setId((long) i);
   tb.setInvestments("investments" + i);
   tb.setName("name" + i);
   list.add(tb);
  }
  table.setRowList(list);
 }

}

package demo.page;

import net.sf.click.control.Column;

public class Tb {
 private Long id;
 private String name;
 private String email;
 private String investments;

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public String getInvestments() {
  return investments;
 }

 public void setInvestments(String investments) {
  this.investments = investments;
 }
}

页面

home.htm

 $imports

$table 

 $form  
 #if ($msg)
<div id="msgDiv">$msg</div>
#end

模版页面 border-template.htm

<html>
<head>
<title>$title</title>
<link rel="stylesheet" type="text/css" href="$context/style.css" title="Style" />

</head>

<body>

<div id="header"> $title </div>

<div id="container">#parse($path)</div>
 
</body>
</html>

5 sruts例子

package struts2.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

 public static final String MESSAGE = "测试Struts is up and running ...";

 public String execute() throws Exception {
  setMessage(MESSAGE);
  return SUCCESS;
 }

 private String message;

 public void setMessage(String message) {
  this.message = message;
 }

 public String getMessage() {
  return message;
 }
}
页面HelloWorld.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
   
<%@ taglib prefix="s" uri="/struts-tags"%>

<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h2><s:property value="message" /></h2>
 
</body>
</html>

6 目录结构