本文将通过一个前台通过表单输入user,来实现hello user的方式来讲述Struts2中使用注解。
首先要引入以下jar包
commons-fileupload-1.2.1 commons-io-1.3.2 commons-logging-1.1 freemarker-2.3.13 junit-3.8.1 onl-2.6.11 spring-test-2.5.6 struts2-convention-plugin-2.1.6 struts2-core-2.1.6 xwork-2.1.2
首先要确定一下版本是否正确,此外struts2-convention-plugin-2.1.6 是必须加入的,这个包是用来注解的核心包。
目录的结构如下:
接下来书写表单输入页面
< %@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> < %@taglib uri="/struts-tags" prefix="s" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World</title> </meta></head> <body> <s:form action="welcome-user" > <s:textfield name="userName" label="User Name" /> <s:submit /> </s:form> </body> </html>
需要注意的是表单中action为url的名字。
我们在WelcomeUser 的类中布置好欢迎信息,返回“success”。
package com.vaannila.action;
import com.opensymphony.xwork2.ActionSupport;
public class WelcomeUser extends ActionSupport{
private String userName;
private String message;
public String execute() {
message = "Welcome " + userName;
return SUCCESS;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setMessage(String message) {
this.message = message;
}
public String getUserName() {
return userName;
}
public String getMessage() {
return message;
}
}
这里有没有发现我们action的url和名字有没有相似之处呢?
我们再welcome-user.jsp 显示返回的欢迎信息。如下:
< %@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
< !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=ISO-8859-1">
<title>Welcome User</title>
</meta></head>
<body>
<h1>${message}</h1>
</body>
</html>
配置web.xml
< ?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>Struts2Example1</display> <filter> </filter><filter -name>struts2</filter> <filter -class> org.apache.struts2.dispatcher.ng.filter. StrutsPrepareAndExecuteFilter </filter> <filter -mapping> </filter><filter -name>struts2</filter> <url -pattern>/*</url> <welcome -file-list> </welcome><welcome -file>index.jsp</welcome> </web>
部署执行下http://localhost:8080/Struts2Example1,我们可以看到。
随便输入一个名字,即可得到如下
接下来,我们看下它是如何进行的。
Convention plug-in是在后台运行的。
- considered as the root package for the Convention plug-in.
默认下,Convention plug-in 会在struts,struts2,action或者actions等包中进行查找。这里,我们的包名为com.vaannila.action。任何满足以上条件的的包都将被引入。 - action类应该要继承com.opensymphony.xwork2.Action接口或者action类名以Action结尾。我们的例子中,WelcomeUser继承于com.opensymphony.xwork2.ActionSupport,从而也是实现了com.opensymphony.xwork2.Action。
- Convention plug-in使用action的类名来映射成对应的URL。例子中的类名为WelcomeUser,它对应的URL为welcome-user.
- 接下里看看下results,他并不知道文件在哪个目录下面,但Convention plug-in将根据Action名来进行判断,例如,如何返回“success”,那么将自动查找文件名为welcome-user-success.jsp 或 welcome-user.jsp。
Source :Download
Source + Lib :Download
原文地址:
http://www.vaannila.com/struts-2/struts-2-example/struts-2-annotation-example-1.html



