`

Struts2: HelloWord

阅读更多
Struts2官网: http://struts.apache.org/
Struts2官网示例:http://code.google.com/p/struts2-examples/downloads/list
一 运行环境: XP3+Myeclipse6.6+Tomcat7+JDK6
二 使用版本: struts-2.2.1.1,所必需jar,参见附件

三 具体代码:
1  JSP页面代码:
A index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>  
<%
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>Struts2 HelloWorld</title>	
  </head>  
  <body>
    <a href="<s:url action="hello"/>">Hello</a>
  </body>
</html>

B HelloWord.jsp
<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>  
<%
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>Struts2 Hello</title>
    </head>  
  <body>
      <s:property value="name"/><br>
      <a href="${pageContext.request.contextPath}">返回</a>  
      <s:debug></s:debug>  
  </body>
</html>


2 HelloWordAction.java
package net.liuzd.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;	
	private String name = null;	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String execute() throws Exception {	
		name = "咫尺天涯";
		return SUCCESS;
	}
}


3 配置文件
Struts2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	  <constant name="struts.devMode" value="true" />
	  <package name="hello" extends="struts-default" namespace="/">
	  	 <action name="hello" class="net.liuzd.struts2.HelloWorldAction">
	  	 	<result>/hello.jsp</result>
	  	 </action>
	  </package>	  
</struts>  


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">
    <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.jsp
        </welcome-file>
    </welcome-file-list>
</web-app>
  • 大小: 9.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics