IdeaIU 12 手工配置 Struts2
一、下载jar包
从Apache软件基金会的struts2项目网站下载框架的jar包,
二、导入jar包
File → Project Structure →Project Settings → Libraries
添加所需的11个基本jar包
三、配置web.xml
在<web-app>标签内添加以下内容
<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>
四、编写Action类
public class HelloAction implements Action {
private String message;
public String getMessage() {
return message;
}
@Override
public String execute() throws Exception {
message="HelloWorld!";
return SUCCESS; //To change body of implemented methods use File | Settings | File Templates.
}
}
五、编写jsp界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>显示HelloWorld</title>
</head>
<body>
<h2>
<s:property value="message"></s:property></h2>
</body>
</html>
六、编写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>
<package name="default" extends="struts-default">
<action name="HelloWorld" class="com.sinitek.training.pm.action.HelloAction">
<result name="success">SayHelloWorld.jsp
</result>
</action>
</package>
</struts>
注意这里加粗的action name,HelloWorld是浏览器地址栏中显示的间接路径名,这在Tomcat配置默认加载页时也会出现
七、添加struts.xml与struts-default.xml的模块引用
File → Project Structure →Project Settings → Modules → <项目名称> → Web
添加Struts 2,然后选中Struts 2。继续添加struts.xml与struts-default.xml。结果如下:
八、配置Tomcat的默认页面
Run → Edit Configurations
把startup page修改一下,路径加上之前设置的HelloWorld