最近开始学习Struts2,一个官网的HelloWorld居然搞了两天,一大堆莫名其妙的问题。万事开头难,今天终于把所有bug处理掉了。
Struts2 官网的案例
http://struts.apache.org/docs/hello-world-using-struts-2.html
搭建Tomcat开发环境
推荐两篇验证过可行没问题的文章:
MyEclipse:
http://www.tuicool.com/articles/32ea6v
Eclipse:
http://jingyan.baidu.com/article/ca2d939dd90183eb6d31ce79.html
MyEclipse和Eclipse用起来其实没多大差别,感觉MyEclipse会比较人性化一点,Eclipse是开源的免费的,不需要破解,具体选择看你个人习惯。
下载jar包
可以去官网下载:http://struts.apache.org/
Jar包直接复制粘贴到WEB-INF/lib目录下即可。
项目结构
其实src目录就是WEB-INF/classes目录,大概是IDE工具为了编程方便习惯把它放出来而已。
index.jsp :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<h3>This is index.jsp</h3>
<p><a href="<s:url action='hello'/>">Hello World</a><br></p>
</body>
</html>
HelloWorld.jsp :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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>
<h1>Congratulations!</h1>
<h3>This is HelloWorld.jsp</h3>
<h3>Message:<s:property value="messageStore.message" /></h3><br>
</body>
</html>
HelloWorldAction.java :
package org.apache.struts.helloworld.action;
import org.apache.struts.helloworld.model.MessageStore;
import com.opensymphony.xwork2.ActionSupport;
/**
* Acts as a Struts 2 controller that responds
* to a user action by setting the value
* of the Message model class, and returns a String
* result.
* @author Bruce Phillips
*
*/
public class HelloWorldAction extends ActionSupport {
private static final long serialVersionUID = 1L;
/**
* The model class that stores the message
* to display in the view.
*/
private MessageStore messageStore;
/*
* Creates the MessageStore model object and
* returns success. The MessageStore model
* object will be available to the view.
* (non-Javadoc)
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
public String execute() throws Exception {
messageStore = new MessageStore() ;
return SUCCESS;
}
public MessageStore getMessageStore() {
return messageStore;
}
public void setMessageStore(MessageStore messageStore) {
this.messageStore = messageStore;
}
}
MessageStore .java
package org.apache.struts.helloworld.model;
/**
* Model class that stores a message.
* @author Bruce Phillips
*
*/
public class MessageStore {
private String message;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MessageStore() {
setMessage("Hello Struts User");
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
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>Hello World Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
你可能在其他教程看到这里的写的是org.apache.struts2.dispatcher.FilterDispatcher而感到迷惑,说明一下:
- org.apache.struts.action.ActionServlet 是struts1.x的过滤器
- org.apache.struts2.dispatcher.FilterDispatcher 是struts2.0.x到2.1.2版本的核心过滤器;
- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter是自2.1.3开始就替代了FilterDispatcher的;
如果程序报类找不到异常,且后面有 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
等信息,可能是你的版本问题.解决方法是确定你的struts版本。
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="basicstruts2" extends="struts-default">
<!-- If no class attribute is specified the framework will assume success and
render the result index.jsp -->
<!-- If no name value for the result node is specified the success value is the default -->
<action name="index">
<result>/index.jsp</result>
</action>
<!-- If the URL is hello.action the call the execute method of class HelloWorldAction.
If the result returned by the execute method is success render the HelloWorld.jsp -->
<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
运行结果
http://localhost:8080/struts-examples/index.jsp
http://localhost:8080/struts-examples/hello.action
源代码见评论链接