1.IDEA配置maven、tomcat( https://blog.csdn.net/weixin_41715878/article/details/83211564)
2.导包
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.4</version>
</dependency>
3.在web.xml配置过滤器
<!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 version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Archetype Created Web Application</display-name>
<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.在resources下新建、配置struts.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>
<!--在struts配置太多的package查看不方便,通过再写一个test.xml(任意名字),引入到struts.xml中-->
<!--test.xml配置同样需要约束-->
<!--<include file="test.xml"/>-->
<!-- namespace="/" 表示请求中action="/hello.action"中的/ -->
<package name="default" extends="struts-default" namespace="/">
<!--如果没有找到指定的action,会跳转到错误页面,default-action-ref需要配置在package里面第一行-->
<!--name指定一个action-->
<default-action-ref name="myError"/>
<!--全局结果集,必须在action节点上面配置-->
<global-results>
<!-- 当使用action时,内部没有result时,就会调用全局的result-->
<result name="index">/index.jsp</result>
</global-results>
<!-- name="hello" 对应请求中actino="/hello -->
<action name="show" class="com.ssh.action.UserAction" method="show">
<!-- name="success"对应show方法中的返回值 -->
<!-- type='dispatcher' 默认结果类型,forword请求跳转到另一个jsp-->
<!-- type='chain' 请求跳转到另一个action-->
<!-- type='redirect' 重定向一个jsp页面-->
<!-- type='redirectAction' 重定向一个action-->
<!-- type='stream' 将元素数据作为流传递回浏览器,该结果类型对下载的内容和图片非常有用-->
<result name="success" type="dispatcher">/WEB-INF/main.jsp</result>
</action>
<!--使用通配符方式配置执行方法-->
<!--{1}表示第一个通配符,可以在result中共用-->
<!--<action name="show_*_*_*" class="com.ssh.action.{1}" method="{2}">
<result>/{3}.jsp</result>
</action>-->
<action name="myError">
<result>/error.jsp</result>
</action>
</package>
</struts>
3.编写Action类
public class UserAction implements ModelDriven<User> {
/*使用模型驱动方式,可以直接把表单数据封装到实体类的对象里面*/
/*需要实现类ModelDriven<User>,和方法getModel*/
/*form表单中input的name值需要和user成员变量名称一致*/
private User user = new User();
@Override
public User getModel() {
return user;
}
public String show(){
/*action获取表单的三种方式*/
/*ActionContext中保存了Action执行所需要的所有对象,包括parameters、request、session、application等*/
/*ActionContext context = ActionContext.getContext();
HttpSession session = context.getSession();*/
/*ServletActionContext类,可以获得域对象*/
/* HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletContext context = ServletActionContext.getServletContext();
PageContext pageContext = ServletActionContext.getPageContext();*/
/*接口注入ServletRequestAware、ServletResponseAware、SessionAware、ServletContextAware*/
/*略*/
return "success";
}
public String test(){
System.out.println(user);
return "success";
}
}