eclipse中建立项目 html
搭建步骤:java
1.建立web项目web
2.下载导入相关jar包apache
3.建立并完善相关配置文件浏览器
4.建立(控制器)Action 并测试启动tomcat
1.文件--新建--动态web项目服务器
给项目起一个名字 而后选择项目的服务器运行环境 这里须要添加tomcat的目录 有的话请忽略app
而后一直下一步 完成 这样就建立了一个java web项目eclipse
2.引入Struts须要的jar包 jsp
须要访问apach struts的下载网站
1.http://struts.apache.org/
解压下载的压缩包 从lib里面选择下面的几个基础包 拷到项目的WEB-INF 下的lib中
而后将导入的包添加引用到项目中
选择Add JARs 添加jar包
而后全选下 点击OK就行了
配置相关文件
(1)web.xml 的配置(添加一个过滤器filter)
<filter> <filter-name>struts</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
(2)建立struts核心文件--struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > 3 <struts> 4 5 </struts>
完成配置文件
4.建立一个Action(class文件)
建立一个类文件 继承com.opensymphony.xwork2.ActionSupport
这样就建立了一个Action 类文件
1 package com; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class HelloWorld extends ActionSupport { 6 7 }
建立完成以后如何去执行它的方法呢 这里struts2有一个默认的方法
或者在action中 alt+/ 找到execute 方法 而后在里面打印输出一句话
package com; import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extends ActionSupport { @Override public String execute() throws Exception { System.out.println("执行Action"); return SUCCESS; } }
而后咱们继续配置咱们的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="default" namespace="/" extends="struts-default"> <action name="helloworld" class="com.HelloWorldAction"> <result>/result.jsp</result> </action> </package> </struts>
result默认是返回sucess的
而后咱们建立返回显示的file result.jsp
<%@ page language="java" contentType="text/html; 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
这是result.jsp
</body>
</html>
到此 咱们一个项目就简单配置完成了
而后咱们发布下 选择项目右键 Debug As --Debug on Server 选择咱们的tomcat服务器 而后完成
而后在浏览器中访问网址:http://localhost:8080/StrutsDemo/HelloWorld.action
这样咱们就完成了在eclipse中的简单搭建