struts2.0配置及使用

运行不了可能问题(后面附有一个成功的代码):

1、 workspace目录之中只能存在英文,不能包含数字、空格和中文;

2、 建web工程之前,打开C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps目录,删除与你同名的工程。

3、 注意新创建的web工程的web.xml文件开头与struts框架需要的web.xml开头是不一样的。切忌注意!!!

实验内容和步骤:

1.在MyEclipse 中创建Web工程(假设工程名ssh1)。

2Step-1MyEclipse里添加Struts2依赖的库文件(本课程还提供Struts 2.0.11版本,它需要依赖5个库文件,分别是

struts2-core-2.0.11.jar

xwork-2.0.4.jar

ognl-2.6.11.jar

freemarker-2.3.8.jar

commons-logging-1.0.4.jar)

放入WEB-INF/lib目录下。

Step-2 在WEB-INF/web.xml中,添加Struts2MVC启动框架的过滤器配置(以Struts2.1.6为例):

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

<web-app version="2.5"

xmlns="http://java.sun.com/xml/ns/javaee"

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

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

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

说明和注意的问题:

Struts 2版本 < 2.1.3,过滤器的类名(filter-class)org.apache.struts2.dispatcher.FilterDispatcher,

而当Struts 2版本>=2.1.3,过滤器的类名(filter-class)改为org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExec

helloworld.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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>My JSP 'helloworld.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<form action="hello2.action" method="post">
姓名:<input type="text" name="username">
<input type="submit" value="确定">
</form> <br>
</body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
访问成功!!! <br>
</body>
</html>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.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>


package helloWorld;

HelloWorldAction.java
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {

String username;
public String getUsername(){
return this.username;
}

public void setUsername(String username){
this.username=username;
}


public String execute() throws Exception {

return SUCCESS;

}

}


uteFilter。-----------(转自朱老师的课件)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值