1.新建一个web项目
2.导入struts2的jar包
http://struts.apache.org/download.cgi#struts25101 官网下载
最基本jar包可以导入
struts-2.3.28\apps\struts2-blank.war
如果需要返回json,加上struts-2.3.28\lib\struts2-json-plugin-2.3.28.jar
3.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>StrucsProject</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>
<!-- 配置strucs2-->
<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>/*</url-pattern>
</filter-mapping>
</web-app>
4.src下新建struts2.properties 配置常量
可以调用一个action中多个方法
struts.enable.DynamicMethodInvocation=true
5.新建pojo类
package com.hello.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.hello.model.Student;
import com.opensymphony.xwork2.ActionSupport;
public class HelloStruts extends ActionSupport {
private int id;
private String name;
private int other;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOther() {
return other;
}
public void setOther(int other) {
this.other = other;
}
public HelloStruts(int id, String name, int other) {
super();
this.id = id;
this.name = name;
this.other = other;
}
public HelloStruts() {
super();
}
@Override
public String toString() {
return "HelloStruts [id=" + id + ", name=" + name + ", other=" + other
+ "]";
}
private static final long serialVersionUID = L;
private Map<String,Object> dataMap;
public String execute(){
System.out.println(id);
System.out.println(name);
return "cc";
}
public String edit(){
System.out.println("编辑");
return "edit";
}
public String del(){
System.out.println("删除");
return "del";
}
public void getJson(){
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
//response.setCharacterEncoding("UTF-8");
PrintWriter out;
try {
out = response.getWriter();
//JSON在传递过程中是普通字符串形式传递的,这里简单拼接一个做测试
String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"张三\",\"say\":\"Hello , i am a action to print a json!\",\"password\":\"JSON\"},\"success\":true}";
out.println(jsonString);
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String json(){
Student student = new Student("liu", );
dataMap = new HashMap<String, Object>();
dataMap.put("cc", student);
dataMap.put("success", true);
return "success";
}
public Map<String, Object> getDataMap() {
return dataMap;
}
}
6.在src下新建struts2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="struts2" extends="struts-default,json-default">
<action name="HelloStruts" class="com.hello.action.HelloStruts">
<result name="cc">/indexresult.jsp</result>
<result name="edit">/indexresult.jsp</result>
<result name="del">/indexresult.jsp</result>
<result type="json" name="success">
<param name="root">dataMap</param>
</result>
</action>
</package>
</struts>
7.前台页面
<%@ 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>
<base href="<%=request.getContextPath()%>/" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="js/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<script src="js/jquery.min.js" type="text/javascript" ></script>
<script src="js/bootstrap/js/bootstrap.min.js" type="text/javascript" ></script>
<title>Insert title here</title>
</head>
<body>
<div class="contrainer">
<form id="po" action="HelloStruts!edit.action" method="post">
<input type="text" name="id">
<input type="text" name="name">
<input type="submit" value="submit">
</form>
<form id="po" action="HelloStruts!del.action" method="post">
<input type="text" name="id">
<input type="text" name="name">
<input type="submit" value="submit">
</form>
<form id="po" action="HelloStruts.action" method="post">
<input type="text" name="id">
<input type="text" name="name">
<input type="submit" value="submit">
</form>
<form id="po" action="HelloStruts!getJson.action" method="post">
<input type="text" name="id">
<input type="text" name="name">
<input type="submit" value="submit">
</form>
<form id="po" action="HelloStruts!json.action" method="post">
<input type="text" name="id">
<input type="text" name="name">
<input type="submit" value="submit">
</form>
</div>
</body>
</html>
参考:http://kingxss.iteye.com/blog/1622455