Intellij IDEA Struts 2 注解 示例

来源:互联网 发布:数据可视化怎么做 编辑:程序博客网 时间:2024/05/21 07:12

https://www.mkyong.com/struts2/struts-2-hello-world-annotation-example/


demo下载

http://www.mkyong.com/wp-content/uploads/2010/06/Struts2-Hello-World-Annotation-Example.zip


struts2 注解

1. 更新 pom.xml

使用 Struts 2 注解特性, 你需要下载 struts2-convention-plugin.jar.

pom.xml

    <dependency>          <groupId>org.apache.struts</groupId>      <artifactId>struts2-core</artifactId>      <version>2.1.8</version>    </dependency>    <dependency>          <groupId>org.apache.struts</groupId>      <artifactId>struts2-convention-plugin</artifactId>      <version>2.1.8</version>    </dependency>
2. LoginAction

创建一个 LoginAction 继承 ActionSupport

Annotation 版本

package com.mkyong.user.action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.ResultPath;import com.opensymphony.xwork2.ActionSupport;@Namespace("/User")@ResultPath(value="/")@Result(name="success",location="pages/login.jsp")public class LoginAction extends ActionSupport{}

XML 等价

<package name="user" namespace="/User" extends="struts-default">    <action name="Login">        <result>pages/login.jsp</result>    </action></package>
3. WelcomeUserAction

重写 the execute() 方法 并且 指定注解.

Annotation 版本

package com.mkyong.user.action;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.ResultPath;import com.opensymphony.xwork2.ActionSupport;@Namespace("/User")@ResultPath(value="/")public class WelcomeUserAction extends ActionSupport{    private String username;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    @Action(value="Welcome", results={        @Result(name="success",location="pages/welcome_user.jsp")    })    public String execute() {        return SUCCESS;    }}

XML 等价

<package name="user" namespace="/User" extends="struts-default">   <action name="Welcome" class="com.mkyong.user.action.WelcomeUserAction">    <result name="SUCCESS">pages/welcome_user.jsp</result>   </action></package>
4. JSP 视图页面

一旦点击 submit 按钮,
普通 JSP 视图页面 接收 username 和 password,
and 跳转 去 a welcome 视图页面 。

login.jsp

<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %>Struts 2 Hello World Annotation Example

welcome_user.jsp

<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %>Struts 2 Hello World Annotation ExampleHello 
5. struts.xml

不需要创建 struts.xml file, 所有的Class文件都是注解的.

6. web.xml

Just 创建 web.xml 文件 and 声明 the FilterDispatcher filter 。

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>  <display-name>Struts 2 Web Application</display-name>  <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>
7. 运行它

The LoginAction.action is changed to login.action, see “Naming Converter” above.

http://localhost:8080/Struts2Example/User/login.action

Struts 2 注解 login 界面
http://localhost:8080/Struts2Example/User/Welcome.action

Struts 2 annotation welcome screen


demo下载

http://www.mkyong.com/wp-content/uploads/2010/06/Struts2-Hello-World-Annotation-Example.zip


引用

Struts 2 convention plugin 文档
http://struts.apache.org/2.1.8/docs/convention-plugin.html

Strust 2 Hello World (XML 版本)
http://www.mkyong.com/struts2/struts-2-hello-world-example/


其他文章:

struts-2-example
https://dzone.com/tutorials/java/struts-2/struts-2-example/struts-2-annotations-example-1.html

Struts2的注解功能
http://www.cnblogs.com/wayne_wang/archive/2011/01/24/1942927.html


end

原创粉丝点击