一个简单的struts2框架的web应用的开发步骤
1,在Myeclipse下点File-NEW-Web project填写工程名
2,导入struts2框架所必须的包:包括一下六个:
commons-fileupload-1.2.1
commons-logging-1.0.4
freemarker-2.3.15
ognl-2.7.3
struts2-core-2.1.8
xwork-core-2.1.6
到工程的WEB-INF/lib目录下
3,在工程的src目录下新建struts.xml的文件写入一下内容
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
< struts >
< package name ="chen" extends ="struts-default" >
< action name ="hello" class ="Hello" method ="execute" >
< result name ="success" >/WEB-INF/hello.jsp </ result >
</ action >
</ package >
</ struts >
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
< struts >
< package name ="chen" extends ="struts-default" >
< action name ="hello" class ="Hello" method ="execute" >
< result name ="success" >/WEB-INF/hello.jsp </ result >
</ action >
</ package >
</ struts >
4,编辑WEB-INF目录下的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" >
< 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 >
< 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" >
< 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 >
5,在src目录下新建一个Hello.java文件写入以下内容
package chenqiang;
public class Hello {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute(){
message = "Hello world";
return "success";
}
}
public class Hello {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute(){
message = "Hello world";
return "success";
}
}
6,在WEB-INF目录下新建hello.jsp文件,写入以下内容
<%@ page language=
"java" pageEncoding=
"UTF-8"%>
<html>
<head>
<title></title>
</head>
<body>
${message }
</body>
</html>
<html>
<head>
<title></title>
</head>
<body>
${message }
</body>
</html>
到目前为止工程新建完毕,剩下的工作就是打包部署到服务器上,部署好后访问
localhost:8080/工程名/hello,就能在页面上见到hello world了
转载于:https://blog.51cto.com/chenqiangjsj/447866