版本:
eclipse 3.3
,
myeclipse 6.0
,
tomcat 6.0
在
myeclipse
中新建項目
StrutsDemo
,在
struts
官網下載
struts2.0 jar
包
並將
jar
包拷貝到項目
lib
中
若
myeclipse
版本爲
6.5
則可以直接導入
struts2.0
包
核心包:
commons-logging-api-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.8.jar
xwork-2.0.3.jar
在
com.shiryu.demo
包中新建
HelloAction.java
package com.shiryu.demo;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
private String msg;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
if
("kava".equalsIgnoreCase(msg)) {
return
"suc";
} else {
return
"err";
}
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
System.out.print("*******************");
this.msg = msg;
}
}
配置
web.xml
文件
<?xml version="1.0"
encoding="UTF-8"?>
<web-app 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">
<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>
</web-app>
配置
struts.xml
(struts.xml
在
src
文件夾中
)
<?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>
<package name="zhaiyu"
namespace="/" extends="struts-default">
<action name="hello"
class="com.shiryu.demo.HelloAction">
<result
name="suc">
/hello.jsp
</result>
<result
name="err">
/errors.jsp
</result>
</action>
</package>
</struts>
新建
index.jsp
<%@ page language="java" import="java.util.*"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<form action = "hello.action" method =
"post">
輸入內容:
<input type="text" name="msg" />
<input type="submit" value="
提交
"/>
</form>
</body>
</html>
新建
hello.jsp
<%@ 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>
</head>
<body>
輸入內容爲
:<s:property value="msg"/>
</body>
</html>
新建
errors.jsp
<%@ page language="java" import="java.util.*"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'errors.jsp' starting page</title>
</head>
<body>
輸入內容有錯誤!!!!
</body>
</html>