在struts.xml中配置默认Action类(二)

来源:互联网 发布:淘宝刷好评 编辑:程序博客网 时间:2024/04/30 21:22

(一)配置核心代码

       <!-- 1.配置默认Action -->       <default-action-ref name="test"/>        <!--2.引入默认Action的属性值,如果请求资源不存在,都跳到error.jsp-->       <action name="test">          <result>/error.jsp</result>       </action> 

(二)实例演示

1.创建一个模型类存储信息

package com.wang;public class MessageStore {    private String message;    public MessageStore() {        message = "Hello Struts User";    }    public String getMessage() {        return message;    }}

2.创建一个Action类充当控制器

package com.wang;import com.opensymphony.xwork2.ActionSupport;public class HelloWorldAction extends ActionSupport {    private static final long serialVersionUID = 1L;    private MessageStore messageStore;    public String execute() {        messageStore = new MessageStore() ;        return SUCCESS;    }    public MessageStore getMessageStore() {        return messageStore;    }}

3.创建三JSP页面展示信息

<!DOCTYPE html><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><html>  <head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    <title>Struts2程序演示</title>  </head>  <body>    <h1>欢迎学习 Struts2基础!</h1>    <p><a href="<s:url action='hello'/>">Hello World</a></p>  </body></html> 

<!DOCTYPE html><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><%@taglib prefix="s" uri="/struts-tags" %><html>  <head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    <title>Hello World!</title>  </head>  <body>    <h2><s:property value="messageStore.message" /></h2>  </body></html>
<%@ 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>访问路径无效页面</title></head><body>   <img width="100%" height="70%" src="http://upload-images.jianshu.io/upload_images/7627305-fcebf19731172a95.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"></body></html>

4.配置web.xml

<?xml version="1.0" encoding="UTF-8"?><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">     <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>    <display-name>Struts Blank</display-name>    <!-- 配置struts2的核心过滤器 -->    <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>

5.配置struts.xml将Action类与JSP联系起来

<?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>    <constant name="struts.devMode" value="true" />    <package name="basicstruts2" extends="struts-default">      <!-- 1.配置默认Action -->       <default-action-ref name="test"/>        <!--2.引入默认Action的属性值,如果请求资源不存在,都跳到error.jsp-->       <action name="test">          <result>/error.jsp</result>       </action>        <action name="index">            <result>/index.jsp</result>        </action>        <action name="hello" class="com.wang.HelloWorldAction" method="execute">            <result name="success">/HelloWorld.jsp</result>        </action>    </package></struts>

6.在浏览器地址栏输出错误的URL为:
http://localhost:8080/Struts2-01/HelloStruts

说明:配置默认Action的作用就是处理请求资源不存在,跳转到友好的JSP界面!

结果跳转到如图404页面:
这里写图片描述

原创粉丝点击