struts2环境搭建

struts2环境搭建

环境:ecplise4.5+tomcat7+jdk7+struts2.3.24

1.导入所需jar包,详见附件。

2.配置web.xml

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

 

3.配置struts.xml

<package name="struts2" extends="struts-default">  
        <action name="HelloWorld" class="ssmdemo.HelloWorld">  
            <result>/HelloWorld.jsp</result>  
       </action>  
</package>  

 4.编写action

package ssmdemo;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public final static String MESSAGE = "Struts2 is up and running ...";  
	
	private  String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	} 
	
	 public String execute() throws Exception {
		 setMessage(MESSAGE); 
		 return SUCCESS;
	 }
}

 5.编写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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	 <h2><s:property value="message" /></h2>  
</body>
</html>

 注意:如果没有导入commons-fileupload-1.3.1.jar包  则会报struts-defualts失败错误。

严重: Exception starting filter struts2
Unable to load configuration. - bean - jar:file:/E……/struts2-core-2.3.24.jar!/struts-default.xml:54:89

你可能感兴趣的:(struts2)