日期:2014-05-19  浏览次数:20937 次

Struts2 tag 找不到
在学习,《轻松入门之Struts2 V3.0》,第 1章 Struts2入门,的第一个Helloworld程序就不成功。
代码如下:
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>tutorial</display-name>
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>


2) HelloWorld.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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=ISO-8859-1">
<title>Hello World!</title>
</head>
<body>
<h2><s:property value="message" /></h2>
</body>
</html>


3) Action 类
package org.xmh.demo;

import java.text.DateFormat;
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {
private String message;

public String getMessage() {
return message;
}

public String execute() {
message = "Hello World, Now is "
+ DateFormat.getInstance().format(new Date());
return SUCCESS;
}
}


4) struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="tutorial" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result>/jsp/HelloWorld.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>


我直接运行HelloWorld.jsp,可以运行,没有任何错误,但是并没有出现我期望的结果,即Hello World, Now is xxxx.

------解决方案--------------------
你得访问Action:

http://localhost:8080/../HelloWorld.action

------解决方案--------------------
你直接访问jsp没有经过action自然得不到值栈里的message