1、下载安装jdk7
etc/profile 配置
#set java environment
export JAVA_HOME=/opt/Java/jdk1.7.0_51
export JRE_HOME=/opt/Java/jdk1.7.0_51/jre
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
2、下载安装tomcat
etc/profile 配置
#set tomcat environment
export CLASSPATH=$CLASSPATH:%JAVA_HOME/lib/tools.jar
export TOMCAT_HOME=/home/ubuntu/java/apache-tomcat-6.0.39
export CATALINA_HOME=$TOMCAT_HOME
export PATH=$PATH:$TOMCAT_HOME/bin
tomcat 配置角色 路径:/apache-tomcat-6.0.39/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>
tomcat修改端口号 路径:/apache-tomcat-6.0.39/conf/server.xml
<Connector port="9090" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
3、下载安装elcipse 直接解压即可,第一次使用需要配置服务器
window->property->server->选择自己安装tomcat路径即可
4、下载解压struts2即可
主要配置核心包(在WEB-INF/lib/)
struts2实例目录如下图:
简单例子说明:
web.xml (web-inf/web.xml)配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
<welcome-file-list>
<welcome-file>/demo/demo.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
-------------------------------------------------------------------------------------------------
add.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>add计算器</title>
</head>
<body bgcolor="#ffffc0">
<body>
<form id="calForm" method="post" action="add.action">
<table>
<tr>
<td>第一个数字</td>
<td><input type="text" name="num1" /></td>
</tr>
<tr>
<td>第一个数字</td>
<td><input type="text" name="num2" /></td>
</tr>
<tr>
<td><input type="submit" value="login" /></td>
</tr>
</table>
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------
add_result.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>第一個數字:</td>
<td>${param.num1}</td>
</tr>
<tr>
<td>第一個數字:</td>
<td>${param.num2}</td>
</tr>
<tr>
<td>結果</td>
<td>${sessionScope.result}</td>
</tr>
</table>
<button οnclick="history.go(-1)">返回</button>
</body>
</html>
--------------------------------------------------------------------------------
src/com/laijia/Calculator.java:
package com.laijia;
public class Calculator {
public double add(double a, double b) {
return (double) a + b;
}
public double sub(double a, double b) {
return (double) a - b;
}
}
src/com/laijia/AddAction.java:
package com.laijia;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class AddAction extends ActionSupport{
private static final long serialVersionUID = 2L;
private double num1;
private double num2;
public double getNum1() {
return num1;
}
public void setNum1(double num1) {
this.num1 = num1;
}
public double getNum2() {
return num2;
}
public void setNum2(double num2) {
this.num2 = num2;
}
public String execute() {
Calculator cal = new Calculator();
double result = cal.add(num1, num2);
// 將結果保存在session中
ActionContext.getContext().getSession().put("result", result);
return "cal";
}
}
-------------------------------------------------------------------------------------------
struts.xml(src/):
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--<constant name="struts.enable.DynamicMethodInvocation" value="false"
/> -->
<constant name="struts.devMode" value="true" />
<package name="demo2" extends="struts-default">
<action name="add" class="com.laijia.AddAction" method="execute">
<result name="cal"> /demo/add_result.jsp </result>
</action>
</package>
</struts>
程序访问路径:
http://localhost:9090/struts2_01/demo/add