开发工具:
eclipse+tomcat(9.0)+struts2.5.22
首先从官网下载struts-2.5.22-all.zip压缩包,解压之。
然后新建一个空web项目
其次将struts-2.5.22-all的lib目录下的jar文件复制到web项目WEB-INF下的lib中
接着配置web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>struts2</display-name>
<welcome-file-list>
<welcome-file>index.html</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>
<!-- Filters -->
<!-- START SNIPPET: filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- END SNIPPET: filter -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
然后再src目录下建一个名为struts.xml的文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
</struts>
然后我们就可以开始开发项目了!!
下面是演示实例:
1.再src下新建一个包,编写一个java文件:
代码如下:
package cn.javass.hello.struts2impl.action;
import com.opensymphony.xwork2.Action;
public class HelloWorldAction implements Action{
private String account;
private String password;
private String submitFlag;
public String execute() throws Exception{
System.out.println("用户输入的参数为==="+"account="+account+"password="+password+"submitFlag="+submitFlag);
return "toWelcome";
}
public String getAccount()
{
return account;
}
public void setAccount(String account)
{
this.account=account;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password=password;
}
public String getSubmitFlag()
{
return submitFlag;
}
public void setSubmitFlag(String submitFlag)
{
this.submitFlag=submitFlag;
}
}
2.然后编写struts.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<constant name="struts.locale" value="zh_CN"/>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="struts2" extends="struts-default">
<action name="helloworldAction" class="cn.javass.hello.struts2impl.action.HelloWorldAction">
<result name="toWelcome">/Text01/welcome.jsp</result>
</action>
</package>
</struts>
3.再WebContent下建立一个Text01的文件夹,在其中编辑jsp文件:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<form action="/struts2/helloworldAction.action" method="post">
<input type="hidden" name="submitFlag" value="login"/>
Account :<input type="text" name="account"/><br/>
PassWord:<input type="password" name="password"/><br/>
<input type="submit" value="submit">
</form>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<%@taglib prefix="s" uri="/struts-tags" %>
欢迎账号为<s:property value="account"/>的朋友来访
</body>
</html>
运行结果: