在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页面:
阅读全文
0 0
- 在struts.xml中配置默认Action类(二)
- 6.在struts.xml中配置默认的action,也就是主页
- 在Struts2 中 struts.xml的Action配置详解
- Struts2 在struts.xml配置Action
- Struts2-action在struts.xml中的配置
- struts.xml中Action的配置详解
- struts.xml中配置一个action调用另一个action
- Struts2 两个Action 动态传参数,在struts.xml中配置
- Struts2框架中struts.xml文件的package标签的命名空间属性、默认action配置、result配置、Struts异常处理
- Struts中配置Action
- struts中配置action
- Struts2 在Struts.xml配置Action的result
- struts-config.xml 中action 属性配置中的scope问题
- Struts2中struts.xml的Action配置详解
- Struts2中struts.xml的Action配置详解
- Struts2中struts.xml的Action、namespace配置详解
- [转]Struts2中struts.xml的Action配置详解
- Struts2中struts.xml的Action配置详解
- 判断IE版本、系统版本、系统平台
- java 关键基础
- 企业大型互联网分布式架构{Java分布式架构 dubbo + springmvc + mybatis + ehcache + redis }
- 新会员注册页面提示会话已过期请重新登录
- 八种排序方法(一)——选择排序
- 在struts.xml中配置默认Action类(二)
- 控制翻转与依赖注入的区别
- java 代码实例
- ssm常用配置
- 数据结构----学期总结
- 16周udp
- Redis用Java代码访问:Key、String、List、Set、Hash、SortSet
- 小结五
- ***leetcode 84. Largest Rectangle in Histogram