您的位置:首页 > 编程语言 > Java开发

struts学习之HelloWorld

2016-03-23 16:32 609 查看
下载struts:

点击打开链接
http://struts.apache.org/download.cgi
1、在java EE下新建dynamical web project工程

2、拷贝jar包到WEB-INF lib文件夹下

jar包为D:\Jdk-eclipsejee-tomcat-ant\struts\struts-2.3.24.1-all\struts-2.3.24.1\apps 中的struts2-blank.war,WEB-INF lib文件夹中。

3、web.xml文件添加核心过滤器,做为前端控制器

<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>

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>test0323</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-list>
</web-app>


4、添加struts.xml文件,默认放在WEB-INF/classes路径下

<?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="HelloAction">
<result>/index.jsp</result>
</action>
</package>

<!-- Add packages here -->

</struts>


5、新建jsp文件:index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>
hello world
</body>
</html>


6、创建Action类 HelloAction.java

public class HelloAction {
public String execute(){

System.out.println("hello world");
return null;
}

}


运行:无需开启tomcat,直接将tomcat部署到eclipse,选择run as:run on server

运行结果为



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: