【struts2】struts2的全局结果处理和依赖注入案例
2013-02-28 09:57
288 查看
web.xml
index.jsp
sc.jsp
LoginAction.java
struts.xml
当没有
添加上
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 添加启动struts2MVC框架的过滤器 --> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div align="center"> <h3> 登陆操作 </h3> <form action="${pageContext.request.contextPath }/csdn/login.action" method="post"> 用户名: <input type="text" name="name" /> <br /> 密码: <input type="password" name="pass" /> <br /> <input type="submit" value="登陆"> </form> </div> </body> </html>
sc.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'sc.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div align="center"> <h3>登陆成功!</h3> </div> </body> </html>
LoginAction.java
package www.csdn.struts_action.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; // 接受客户端的值 private String name; private String pass; private String upload; public String getName() { return name; } // 注入 public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public String getUpload() { return upload; } public void setUpload(String upload) { this.upload = upload; } public String login() { System.out.println("处理业务....."+name+"--"+pass+"---"+upload); return SUCCESS; } }
struts.xml
<?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> <!-- ${pageContext.request.contextPath }/csdn/login.action url:http://localhost:8080/struts_action/csdn/login.action struts-default 包名 --> <package name="user" namespace="/csdn" extends="struts-default"> <!-- 全局的结果集 --> <global-results> <result name="success" type="dispatcher">/sc.jsp</result> </global-results> <!-- <default-class-ref class="com.opensymphony.xwork2.ActionSupport" /> Method=execute --> <action name="login" class="www.csdn.struts_action.action.LoginAction" method="login"> <param name="upload">/images</param> <!-- name="success" <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> --> <result name="success" type="dispatcher">/index.jsp</result> </action> </package> </struts>
当没有
<result name="success" type="dispatcher">/index.jsp</result>跳转到sc页面,
添加上
<result name="success" type="dispatcher">/index.jsp</result>则跳转到index页面
相关文章推荐
- struts2异常处理,global-results定义全局结果处理
- 4、struts2异常处理及全局异常与结果剖析、struts2模型驱动及分层体系架构
- Struts2中防止表单重复提交,global-results定义全局结果处理
- struts2中的依赖注入与指定处理请求后缀
- struts2异常处理,global-results定义全局结果处理
- struts2异常处理,global-results定义全局结果处理
- [转]依赖注入(IOC)的详细讲解,附带实例和测试结果(大师兄亲测)
- struts2的结果处理方式
- struts2学习之---配置处理结果
- 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端。前端控制器既可以使用Filter实现(Struts2采用这种方式),也可以使用Servlet来实现(spring MVC框架)。
- struts2全局结果页面配置
- Struts2的处理流程及为Action的属性注入值
- struts2 result全局结果集
- Spring依赖注入对于Date类型数据的处理
- struts2笔记之常量配置,全局视图配置,默认配置,属性注入配置
- 第八篇——Struts2的处理结果类型
- 深入详解Struts2——Struts2对依赖注入的支持
- Struts2中的全局结果集
- 配置处理结果---struts2学习笔记
- 提交一个from的时候,struts2处理完返回结果打开一个新页面,用target="blank"