2019独角兽企业重金招聘Python工程师标准>>>
项目结构:
1、用IDEA新建一个SpringBoot+Maven的项目
2、新建的项目是没有webapp、WEB-INF、与web.xml文件的,需要在下图中添加:
3、在pom.xml引入struct2
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.18</version>
</dependency>
4、编写主页home.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<form action="hello">
<label for="pername">Please enter your name</label>
<input id="pername" type="text" name="name" />
<input type="submit" value="提交" />
</form>
</body>
</html>
该页面输入一个名字,通过名为hello的action,将名字显示在另一个页面。
5、定义动作HelloWorldAction.java
public class HelloWorldAction {
private String name;
public String excute(){
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
6、jsp页面与动作之间需要通过WEB-INF\classes\struts.xml关联,且需要在web.xml中定义过滤器
struts.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 设置struts是否为开发模式,默认为false,测试阶段一般设为true. -->
<constant name="struts.devMode" value="true" />
<package name="suibian" extends="struts-default">
<action name="hello" class="com.owlforest.home.action.HelloWorldAction" method="excute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
7、编写跳转页HelloWorld.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>show name</title>
</head>
<body>
Hello World : <s:property value="name" />
</body>
</html>
需要注意的是,home.jsp、HelloWorldAction.java与HelloWorld.jsp中的name属性需要在名称上面保持一致。
form表单中的action名字与struct.xml中的action name要保持一致
这个地方在属性过多的情况下可能会出现命名冲突导致错误出现,不知道struct2是如何解决的,后续再继续学习。
8、result的type参数
上面的struts.xml配置的result为:
<result name="success">/HelloWorld.jsp</result>
这实际上时一种简写的形式,完整的形式为:
<result name="success" type="dispatcher">
<param name="location">/HelloWorld.jsp</param>
</result>
dispatcher结果类型是默认的类型。它用于转发到服务器上的servlet,JSP,HTML等页面。它使用RequestDispatcher.forward()方法。
除了上述的dispatcher类型外,还有redirect、freemarker类型。
redirect结果类型调用标准的response.sendRedirect()方法,使得浏览器向给定的位置创建一个新请求。
<result name="success" type="redirect">
<param name="location">https://www.baidu.com</param>
</result>
freemarker是一个流行的模板引擎,使用预定义的模板生成输出。这与JSP类似。使用方法如下:
在webapp下新建文件 hello.fm
,内容为:
What's your name ?${name}
修改struts.xml即可完成配置。
<result name="success" type="freemarker">
<param name="location">/hello.fm</param>
</result>