第一个struts2程序

来源:互联网 发布:2016年就业数据 编辑:程序博客网 时间:2024/06/05 02:25

第一个struts2程序
1.创建动态的web项目
项目名称:chapter01
2.导入struts2jar文件
Strus2jar基础文件
3修改web.xml

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>chapter01</display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <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></web-app>

4在src目录下创建struts.xml文件

<?xml version="1.0" encoding="UTF-8"?><!-- 指定Struts2配置文件的DTD信息 --><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><!-- Struts2配置文件的跟元素 --><struts>    <!-- struts2的action必须放在指定的包空间下定义 -->    <package name="hello" namespace="/" extends="struts-default">    <!--定义action,该action对应的类为cn.itcast.action.HelloWorldAction类 -->    <action name="helloworld" class="cn.itcast.action.HelloWorldAction">    <!--定义处理结果和视图资源之间的映射关系  -->    <result name="success">/success.jsp</result>    </action>    </package></struts>

5编写action类文件

package cn.itcast.action;import com.opensymphony.xwork2.ActionSupport;public class HelloWorldAction extends ActionSupport {    //返回值对应着struts.xml文件中的<result>标签中name属性的值,会执行对应处理结果的视图资源    public String execute() throws Exception{        return SUCCESS;    }}

5创建success.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"    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=UTF-8"><title>Insert title here</title></head><body>欢迎学习第一个Struts2程序</body></html>

6运行程序在url地址栏输入http://localhost:8080/chapter01/helloworld.action

0 0
原创粉丝点击