1、file => new => dynamic web project
2、输入project name,点击next,default output folder中输入WebRoot\WEB-INF\classes,点击next,content directory中输入WebRoot,并勾选下面的generate web.xml deployment descriptor,点击finish
3、在eclipse中部署struts开发包
将开发所需的jar包复制到lib文件夹中,所需jar包路径struts-2.3.34\apps\struts2-blank.war, 如下图所示。
此外,还需要引入另外两个jar包,路径struts-2.3.34\lib,如下图所示。
4、编写工程配置文件
打开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" id="WebApp_ID" version="3.1">
<display-name>Library</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.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>
</web-app>
5、添加struts.properties文件
右击src => new => other,在wizards文本框中输入file,选择file,点击next,在file name框中输入struts.properties,点击finish,创建成功,打开struts.properties,输入如下代码。
<struts.i18n.encoding value="UTF-8"/>
6、编写struts.xml控制器文件
右击src => new => other,在wizards文本框中输入xml,选择xml,点击next,输入struts.xml,点击finish,创建成功,打开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>
<package name="default" namespace="/" extends="struts-default">
<action name="hello" class="com.action.TestAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
7、开发前端页面index.jsp和success.jsp
在WebRoot下创建index.jsp和success.jsp
在index.jsp中输入以下代码。
<%@ page language="java" contentType="text/html; charset=UTF-8"
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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:a action="hello">hello</s:a>
</body>
</html>
在success.jsp中输入以下代码。
<%@ page language="java" contentType="text/html; charset=UTF-8"
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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:property value="helo"/>
</body>
</html>
8、开发后台struts处理程序TestAction.java
在src下创建包com.action,然后在包下面创建TestAction.java文件,输入如下代码。
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String helo;
public String getHelo() {
return helo;
}
public void setHelo(String helo) {
this.helo = helo;
}
public String execute() throws Exception {
helo = "hello,world";
return SUCCESS;
}
}
9、运行测试