一、解压struts-2.3.31apps路径下struts2-blank.rar文件。将其中WEB-INFl路径下的lib中的包和web.xml文件复制到新项目中的WEB-INF路径下。web.xml中的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</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> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
二、struts2-blank.rar解压文件src中的strus.xml文件复制到新项目中的src目录下,配置如下:
<?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> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="index" class="com.itnba.maya.controller.IndexAction">//class中路径与对应控制接收数据的类 <result>//与IndexAction中return对应,当return success时,name可以不填 index.jsp//跳转到index.jsp页面 </result> <result name="error">//当return error时 index_error.jsp </result> <result name="haha">//自定义的 return "haha" index_haha.jsp </result> </action> <action name="home"> <result> home.jsp </result> </action> </package> <!-- Add packages here -->
在src路径下建立一个包com.itnba.maya.controller,在其中建立一个IndexAction类配置如下:
package com.itnba.maya.controller;
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction extends ActionSupport {
private String msg;//用get 来接收传过来的值
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String execute() throws Exception {
// TODO 自动生成的方法存根
return SUCCESS;
}
}
建两个jsp页面index.jsp reg.jsp
reg.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>Insert title here</title> </head> <body> <form action="index" method="post"> 输入<input type="text" name="msg"><br> <input type="submit"> </form> </body> </html>
index.jsp如下:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> this is index.jsp ${msg } </body> </html>
运行reg.jsp 输入 hello world
提交结果如下