struts2.3 创建工程

1:在该网站下载struts2.3.16.3,目前为最新版。http://www.struts.apache.org/download.cgi

不妨下载“Full Distribution”版本

下载完后解压。

2.

用eclipse创建一个Dynamic Web project项目

这里要注意一点,创建的src文件的.class文件要放到WEB-INF/classes下。那么怎么放拟?按照如下图操作。

file-》new -》Dynamic Web project -》输入项目名称(hellostruts),点击next。这里要注意了,看图。

将图中下面的build classes 改为WebContent/WEB-INF/classes    WebContent\WEB-INF\classes

点击完成

 

3.

然后把一些第一步解压出来的必要的组件添加到该项目的WebContent/WEB-INF/lib下。

面对这108个组件我们该如何选择?有时候选择多了不一定是好的;这里我只选择了必要的9个jar文件

整个工程结构如图,路劲要一致:

4.接下来是编写jsp页面了

index.jsp

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <%@ taglib prefix="s" uri="/struts-tags"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11     <s:a action="hello">hello</s:a>
12 </body>
13 </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="hello"/>
</body>
</html>

 

web.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3 xmlns="http://java.sun.com/xml/ns/javaee" 
 4 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
 7 id="WebApp_ID" version="3.0">
 8     <filter>
 9         <filter-name>struts2</filter-name>
10         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
11     </filter>
12  
13     <filter-mapping>
14         <filter-name>struts2</filter-name>
15         <url-pattern>/*</url-pattern>
16     </filter-mapping> 
17     <welcome-file-list>
18         <!-- index page -->
19         <welcome-file>index.jsp</welcome-file>
20     </welcome-file-list>
21 </web-app>

接着是在src目录下建立一个struts.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 <struts>
 6     <package name="default" namespace="/" extends="struts-default">
 7         <action name="hello" class="com.action.TestAction">
 8             <result>/success.jsp</result>
 9         </action>
10     </package>
11 </struts>

struts.properties

<struts.il8n.encoding value="UTF-8"/>

 

接着,在src目录下建立一个包,然后在这个包下new一个类;

TestAction.java

 1 package com.action;
 2 import com.opensymphony.xwork2.ActionSupport;
 3 
 4 public class TestAction extends ActionSupport {
 5     private static final long serialVersionUID=1L;
 6     private String hello;
 7     public String getHello(){
 8         return hello;
 9     }
10     public void setHello(String hello){
11         this.hello=hello;
12     }
13     public String execute() throws Exception{
14         hello="hello world";
15         return SUCCESS;
16     }
17 }

5.运行, 右键工程名--> run as --> run on server

posted on 2016-05-07 18:25  程序员修仙之路  阅读(222)  评论(0编辑  收藏  举报