`
阿尔萨斯
  • 浏览: 4184622 次
社区版块
存档分类
最新评论

【struts2】struts2的全局结果处理和依赖注入案例

 
阅读更多

web.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页面

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics