第一个Struts程序

Struts.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>
	
	<package name="struts" namespace="/test" extends="struts-default">
		<action name="helloworld" class="cn.com.user.HelloWorld" method="execute">
			<result name="success">/WEB-INF/userOK.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">
  <display-name></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.jsp</welcome-file>
  </welcome-file-list>
  
</web-app>

HelloWorld类:

package cn.com.user;


public class HelloWorld {

private String message;



public String getMessage() {
return message;
}






public void setMessage(String message) {
this.message = message;
}






public String execute()
{

message="我的第一个struts应用!";
return "success";

}
}

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>
    <title>Struts</title>
  </head>
  <body> 
  <!-- 调用的是getMessage()方法 -->
  ${message}
  </body>
</html>

你可能感兴趣的:(struts2)