struts2_day01_03_struts2入门案例(一)(二)_05_struts2基本执行过程

来源:互联网 发布:守望先锋性能数据fps 编辑:程序博客网 时间:2024/06/07 10:38

Struts2框架入门


第一步 导入jar包
 



第二步 创建action
 


第三步 配置action类访问路径
(1)创建struts2核心配置文件
- 核心配置文件名称和位置是固定的
- 位置必须在src下面,名称 struts.xml


(2)引入dtd约束
 


(3)action配置
 


访问路径:
http://127.0.0.1/struts2_day01/hello.action
 


第四步 配置struts2过滤器
 

 

  <!-- 过滤器 start -->  <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>    <!-- 过滤器 end -->  

全部代码:

com.hlg.action.HelloAction

package com.hlg.action;public class HelloAction {public String execute(){System.out.println("HelloAction .. execute()...");return "ok";}}

/struts2_day01/WebContent/WEB-INF/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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>struts2_day01</display-name>    <!-- 过滤器 start -->  <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>    <!-- 过滤器 end -->      <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>

/struts2_day01/src/struts.xml


<?xml version="1.0" encoding="UTF-8"?><!-- struts2约束 start --><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><!-- struts2约束 end --><struts><package name="hellodemo" extends="struts-default" namespace="/"> <action name="myhello" class="com.hlg.action.HelloAction"><result name="ok">/hello.jsp</result></action></package></struts>

/struts2_day01/WebContent/hello.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>hello struts2 ..</body></html>

访问效果:http://localhost:8080/struts2_day01/myhello.action



05_struts2基本执行过程



 
原创粉丝点击