码迷,mamicode.com
首页 > 其他好文 > 详细

struts2注解实现页面的跳转

时间:2015-08-01 22:07:07      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:struts2

注解实现的页面跳转其特点是不用配置文件struts.xml因而可以实现零配置。

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>

版权声明:本文为博主原创文章,未经博主允许不得转载。

struts2注解实现页面的跳转

标签:struts2

原文地址:http://blog.csdn.net/zzy1078689276/article/details/47189975

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!