日期:2014-05-20  浏览次数:20713 次

搭建的struts2框架,为什么拿不到message="你好!Struts2!"的值呢
<%@ 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=utf-8">
<title>Struts2 示例</title>
</head>

<body>
<h1><s:property value="message"/></h1>
${message} 
</body>

</html>




package com.sl.java.struts2;

public class HelloWorldAction{
 
public String execute() throws Exception {
return "SUCCESS";
}
private String message="你好!Struts2!";
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}


}



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


<?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>
<include file="struts-default.xml"></include>
<package name="default" extends="struts-default">
<action name="HelloWorld" class="com.sl.java.struts2.HelloWorldAction">
<result name="SUCCESS">/HelloWorld.jsp</result>
</action>
</package>
</struts>


------解决方案--------------------
看了下着代码,我觉得没啥问题......
自己测试了下也正常着,你访问的url有问题吧
应该是访问http://localhost/${yourproject}/HelloWorld.action
不是
http://localhost/${yourproject}/HelloWorld.jsp
------解决方案--------------------
要得到action的值你需要先请求action,也就是你在浏览器输入的地址应该是".action"
------解决方案--------------------
问题出在web.xml中的欢迎页出来就是: HelloWorld.jsp, 这里取不到。。需要利用action请求到后台才能取到。。。
------解决方案--------------------