struts2 注解 demo2

1 必须引入struts2-convention-plugin-2.1.6包
2 WelcomeUserAction 类要放在 action命名的包下 并要用*Action 来命名类
3 successPage.jsp要放在web-inf/results目录下 这个是在struts.properties 根据struts.convention.result.path=/results来配置的

一搭建环境

jdk1.6 struts2.1.6 tomcat6.0
所需包
01.commons-fileupload-1.2.1
02.commons-io-1.3.2
03.commons-logging-1.1
04.freemarker-2.3.13
05.junit-3.8.1
06.ognl-2.6.11
07.spring-test-2.5.6
08.struts2-convention-plugin-2.1.6
09.struts2-core-2.1.6
10.xwork-2.1.2

二代码
web.xml
Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <display-name>Struts2_Annotations2</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <filter>
  11. <filter-name>struts2</filter-name>
  12. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  13. </filter>
  14. <filter-mapping>
  15. <filter-name>struts2</filter-name>
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>
  18. </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>Struts2_Annotations2</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>


WelcomeUserAction.java
Java代码 复制代码 收藏代码
  1. package com.test.action;
  2. import org.apache.struts2.convention.annotation.Action;
  3. import org.apache.struts2.convention.annotation.Result;
  4. public class WelcomeUserAction
  5. {
  6. private String userName;
  7. private String message;
  8. @Action(value = "/welcome", results = { @Result(name = "success", location = "/results/successPage.jsp") })
  9. public String execute()
  10. {
  11. message = "Welcome " + userName + " !";
  12. return "success";
  13. }
  14. public String getUserName()
  15. {
  16. return userName;
  17. }
  18. public void setUserName(String userName)
  19. {
  20. this.userName = userName;
  21. }
  22. public String getMessage()
  23. {
  24. return message;
  25. }
  26. public void setMessage(String message)
  27. {
  28. this.message = message;
  29. }
  30. }
package com.test.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class WelcomeUserAction
{
    private String userName;
    private String message;

    @Action(value = "/welcome", results = { @Result(name = "success", location = "/results/successPage.jsp") })
    public String execute()
    {
        message = "Welcome " + userName + " !";
        return "success";
    }

    public String getUserName()
    {
        return userName;
    }

    public void setUserName(String userName)
    {
        this.userName = userName;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

}



index.jsp在web-info下
Java代码 复制代码 收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib uri="/struts-tags" prefix="s"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Annotations1</title>
  9. </head>
  10. <body>
  11. <body>
  12. <s:form action="welcome">
  13. <s:textfield name="userName" label="User Name" />
  14. <s:submit />
  15. </s:form>
  16. </body>
  17. </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Annotations1</title>
</head>
<body>
<body>
<s:form action="welcome">
	<s:textfield name="userName" label="User Name" />
	<s:submit />
</s:form>
</body>
</html>


successPage.jsp 在web-inf/results
Java代码 复制代码 收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Welcome User</title>
  8. </head>
  9. <body>
  10. <h1>${message}</h1>
  11. </body>
  12. </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome User</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>


struts.properties
Java代码 复制代码 收藏代码
  1. struts.convention.result.path=/results
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值