来源于java视频教程高级教程day37
struts2流程:
开发流程:
1 创建javaweb工程
2 找到开发Struts2应用需要使用到的jar文件.
3 创建jsp文件.
4 创建action文件
5 编写Struts2的配置文件
6 在web.xml中加入Struts2 MVC框架启动配置
1,2
3、jsp文件(test.jsp)
<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
访问路径::
<a href="${pageContext.request.contextPath}/helloWorldAction.action">helloWorld</a><br>
<a href="${pageContext.request.contextPath}/helloWorldAction.action?username='bai'">ddddddd</a><br>
测试struts2 输出helloworld:<br>
<a href="${pageContext.request.contextPath}/primer/helloWorldAction.action">helloWorld</a><br>
测试struts2 输出没有命名空间helloworld:<br>
<a href="${pageContext.request.contextPath}/primer/primer/helloWorldAction.action">helloWorld</a><br>
</body>
</html>
4 action文件(HelloWorldAction.java)
package cn.itcast.web.a_primer;
import com.opensymphony.xwork2.Action;
public class HelloWorldAction implements Action {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public HelloWorldAction(){
System.out.println("这是HelloWorldAction的构造方法");
}
public String execute() throws Exception{
System.out.println(this.getUsername()+"欢迎访问hellowordaction");
return "success";
}
}
5 struts.xml、
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="primer" namespace="/" extends="struts-default">
<action name="helloWorldAction" class="cn.itcast.web.a_primer.HelloWorldAction">
<result name="success">/primer/success.jsp</result>
</action>
</package>
</struts>
6 web.xml、
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<!-- 这是struts2的核心过滤器,如果不使用该过滤器,不能使用struts2进行项目开发 -->
<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>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
遇到的一些问题:
myeclipse中启动tomcat报错
Unrecognized Windows Sockets error: 0: JVM_Bind 异常解决办法
到tomcat目录下的把conf文件夹下的server.xml文件中,找到
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
将该端口修改一下便可
<Connector port="8088" protocol="AJP/1.3" redirectPort="8443" />
org.apache.catalina.LifecycleException: Failed to start component
web.xml文件配置错了。
---------------------------------------------------------------------------------------------
${pageContext.request.contextPath}得到的是项目的相对路径, 如果为根目录那么就是空了,然后通过拦截器拦截bookAdmin然后找到相应的servlet进行处理。---------------------------------------------------------------------------------------------
There is no Action mapped for namespace / and action name helloWorldAction.检查拼写和文件名是否错误。
刚刚还能正常启动的tomcat又报端口被占的错。
win下查看被占用的端口 ”netstat -ano“ 查看全部被占的端口,原来我手动启动过tomcat服务(计算机管理服务中)