1、第一个Struts2程序

为了让我们以后开发方便,我们需要配置struts.xml的dtd的头,这样在编写struts.xml文件中可以有提示!

操作如下:

(1)打开myeclipse→window→Preferences→输入xml→XML→XML Catalog→Add→点击File System找到struts-2.1.7.dtd文件→在Key:输入-//Apache Software Foundation//DTD Struts Configuration 2.0//EN→勾选Specify alternative web address→在复选框下的输入框输入:http://struts.apache.org/dtds/struts-2.0.dtd→ok→ok

1.创建如下项目目录结构

2.在WebRoot下窗前index.jsp文件

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@taglib uri="/struts-tags" prefix="s" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'index.jsp' starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   
24   <body>
25      <h1>
26        <s:property value="message"/>
27      </h1>
28      <hr/>
29      <form action="helloWorld.action" method="post">
30         用户名:<input type="text" name="user"/>
31         <input type="submit" value="提交" />
32      </form>
33      
34   </body>
35 </html>
index.jsp

3.在src包下创建struts.xml文件

步骤:选中src→new→XML(Basic Templates)→next→File name:struts.xml→next→勾选Create XML file from a DTD file→next→勾选Select XML Catalog entry →找到key为:-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN→next →finish

 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="helloWorld" class="com.action.HelloWorldAction">
 8             <result name="success">index.jsp</result>
 9         </action>
10     </package>
11 </struts>
struts.xml

4.编辑WebRoot下的WEB-INF的web.xml文件

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

 

5.在com.actin包下创建HelloWorldAction.java

 1 package com.action;
 2 
 3 import com.opensymphony.xwork2.Action;
 4 
 5 public class HelloWorldAction implements Action {
 6     private String user; //表单中name的属性值
 7     private String message; //错误信息
 8 
 9     public String execute() throws Exception {
10         this.setMessage("Hello,"+this.getUser());
11         return "success";
12     }
13 
14     public String getUser() {
15         return user;
16     }
17 
18     public void setUser(String user) {
19         this.user = user;
20     }
21 
22     public String getMessage() {
23         return message;
24     }
25 
26     public void setMessage(String message) {
27         this.message = message;
28     }
29     
30 
31 }
HelloWorldAction.java

6.运行

 

posted @ 2016-05-17 16:03  红酒人生  阅读(433)  评论(2编辑  收藏  举报