Struts2基本配置
1,新建web项目,导入jar包,新建index.jsp。
<%@ 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>Insert title here</title>
</head>
<body>
hello,
<hr>
用户管理
<a href="user!add.do">添加用户</a>
<a href="user!query.do">查询用户</a>
<a href="user!update.do">修改用户</a>
<a href="user!delete.do">删除用户</a>
</body>
</html>
2,配置web.xml,添加struts能力。
<?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>struts2</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>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3,在src下新建com.action.StrutsAction.java
package com.action;
public class StrutsAction {
public String test(){
System.out.println("test");
return "success";
}
}
4,在src下新建com.action.UserAction.java
package com.action;
public class UserAction {
public String add(){
System.out.println("----add----");
return "success";
}
public String query(){
System.out.println("----query----");
return "success";
}
public String delete(){
System.out.println("----delete----");
return "success";
}
public String update(){
System.out.println("----update----");
return "success";
}
}
5,在src下新建struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<constant name="struts.action.extension" value="do"/>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<package name="struts" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="demo1" class="com.action.StrutsAction" method="test">
<result name="success">/index.jsp</result>
</action>