1. 程式人生 > >struts2註解實現頁面的跳轉

struts2註解實現頁面的跳轉

註解實現的頁面跳轉其特點是不用配置檔案struts.xml因而可以實現零配置。

需要的基本包:

commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.jar
freemarker-2.3.13.jar
junit-3.8.1.jar
ognl-2.6.11.jar
spring-test-2.5.6.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar

struts2零配置所需要的註解外掛包
struts2-convention-plugin-2.1.6.jar

action類:

package cn.mrcast.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
@Results({
	@Result(name="success",location="/success.jsp"),
	@Result(name="error",location="/error.jsp")
})
public class LoginAction extends ActionSupport{
       private String username; 
       private String password;
	   public String getUsername() { 
		  return username;
	   }
	   public void setUsername(String username) {
		  this.username = username;
	   }
	   public String getPassword() {
		  return password; 
	   } 
	   public void setPassword(String password) {
		this.password = password;
	   }
       @Action("login")
	   public String execute() 
	   {
		  if(username.equals("admin")&&password.equals("123456")){
			  return "success";    
		  }  
		  else{ 
			  return "error"; 
		  } 
	    } 
}

index.jsp頁面:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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">
	</head> 
  <body> 
  	<form action="login.action" method="post">
  		使用者名稱:<input type="text" name="username"><br>
  		密  碼:<input type="password" name="password"><br>
  		<input type="submit" value="按鈕">
  	</form> 
  </body>
</html>

success.jsp頁面:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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 'success.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">
  </head>
  <body>  
             登入成功!
  </body>
</html>

error.jsp頁面:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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 'error.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">
  </head>
  <body>
             登入失敗!
  </body>
</html>

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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>*.action</url-pattern>
  </filter-mapping></web-app>