关闭

参考网上的一些struts2的action用法,自己写了一个Demo记录一下。

标签: struts2.0java
35人阅读 评论(0) 收藏 举报
分类:

运行环境为myeclipse2014+tomcat8;下面是项目完成后的整个目录情况。

添加struts2相关架包,可以网上下载自己添加,也可以右键项目点击MyEclipse直接添加相关包。我这里是新建web项目后右键项目点击MyEclipse添加struts2(2.x)pro;这样项目自行添加struts.xml,并且web.xml文件会自行配置完成。

struts.xml代码

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

	<package name="struts2" extends="struts-default">  
            <action name="HelloStruts2" class="com.scyang.action.HelloStruts2">  
                <result>/HelloStruts.jsp</result>  
                <result name="add">/user_add.jsp</result>
            </action>  
        </package> 			
</struts> 

web.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Struts2Demo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.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>

HelloStruts2.java代码

package com.scyang.action;

import com.opensymphony.xwork2.ActionSupport;  
  
public class HelloStruts2 extends ActionSupport  
{  
    public final static String MESSAGE = "Struts2正在运行中!";  
      
    private String message;  
  
  
    /** 
     * @return the message 
     */  
    public String getMessage()  
    {  
        return message;  
    }  
  
  
    /** 
     * @param message the message to set 
     */  
    public void setMessage(String message)  
    {  
        this.message = message;  
    }  
  
  
    public String execute() throws Exception  
    {  
        setMessage(MESSAGE);  
        return SUCCESS;  
    }  
    public String add() throws Exception  
    {  
        setMessage("添加用户成功!");  
        return "add";  
    }  
}  



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>
    <a href="/Struts2Demo/HelloStruts2!execute.action">用户添加</a>
    <br>
    <a href="/Struts2Demo/HelloStruts2!add.action">用户更新</a>   
  </body>
</html>

HelloStruts.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %> 
<%
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 'user_update.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>
    <h2><s:property value="message" /></h2> 
  </body>
</html>

user_add.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'user_add.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>
    <h2><s:property  value="message"/></h2> 
  </body>
</html>


输入http://localhost:8080/Struts2Demo/运行效果如下

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1036次
    • 积分:26
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档