程序下载地址
struts基本结构:
简单的struts–HelloWorld应用主要有四部分:
- src下的struts.xml:
负责配置action,为了方便管理action,引入了包package
<?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" namespace="/test" extends="struts-default">
<action name="helloworld" class="com.hongwei.action.HelloWorldAction" method="execute">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
<action name="addUI">
<result>/WEB-INF/page/employeeAdd.jsp</result>
</action>
<action name="redirect" class="com.hongwei.action.EmployeeUpdateAction" method="execute">
<result name="success" type="redirect">/employeeUpdate.jsp?username=${username}</result>
</action>
<action name="redirectAction" >
<result name="success" type="redirectAction">employeeupdate</result>
</action>
<action name="plainText" >
<result type="plainText">
<param name="location">/plaintext.jsp</param>
<param name="charSet">UTF-8</param>
</result>
</action>
</package>
</struts>
2.WEB-INF下的web.xml
<?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>Struts</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
3.src下的action
负责行为
package com.hongwei.action;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport{
private String msg;
public String getMessage(){
return msg;
}
public String execute(){
msg="这是我的第一struts应用!";
//返回视图名称,默认返回success
return "success";
}
}
- WEB-INF下的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>Hello World page</title>
</head>
<body>
阿斯蒂芬
${message}
</body>
</html>
注意:
- 一般页面都放在WEB-INF下,因为jsp页面仅供action使用,不希望用户直接访问jsp页面。旺旺用户直接访问jsp页面也是没有意义的,因为页面要从action里面获取数据。
- 重定向要访问的页面,要放在WebContent下面,不能放在WEB-INF下面。
- 资源文件只能放在WebContent下面,如 CSS,JS,image等.放在WEB-INF下引用不了.
- 页面放在WEB-INF目录下面,这样可以限制访问,提高安全性.如JSP,html
5.只能用转向方式来访问WEB-INF目录下的JSP,不用采用重定向的方式请求该目录里面的任何资源.如图:index.jsp >> main.jsp