本项目端口号为80
jsp文件
HelloWorld.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 charset= "UTF-8" >
< title> Insert title here< / title>
< / head>
< body>
This is HelloWorld. jsp
< / body>
< / html>
add.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 charset= "UTF-8" >
< title> add< / title>
< / head>
< body>
This is add
< / body>
< / html>
update.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 charset= "UTF-8" >
< title> update< / title>
< / head>
< body>
This is update
< / body>
< / html>
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"
metadata- complete= "false" version= "3.1" >
< absolute- ordering/ >
< display- name> WebXml< / display- name>
< filter>
< filter- name> struts2< / filter- name>
< ! -- 注意你的配置,2.5 版本直接是filter,不是ng. filter -- >
< filter- class > org. apache. struts2. dispatcher. filter. StrutsPrepareAndExecuteFilter< / filter- class >
< / filter>
< filter- mapping>
< filter- name> struts2< / filter- name>
< url- pattern>
struts.xml
第一种方法:指定method属性
< ? xml version= "1.0" encoding= "UTF-8" ? >
< ! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd" >
< struts>
< package name= "default" namespace= "/" extends = "struts-default" >
< ! -- 动态方法调用第一种 指定method属性 -- >
< action name= "helloworld"
class = "com.chen.action.HelloWorldAction"
method= "execute" >
< result name= "success" > / HelloWorld. jsp< / result>
< / action>
< action name= "addAction"
class = "com.chen.action.HelloWorldAction"
method= "add" >
< result name= "success" > / add. jsp< / result>
< / action>
< action name= "updateAction"
class = "com.chen.action.HelloWorldAction"
method= "update" >
< result name= "success" > / update. jsp< / result>
< / action>
< / package >
< / struts>
第二种方法:感叹号方式 不推荐
< ? xml version= "1.0" encoding= "UTF-8" ? >
< ! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd" >
< struts>
< package name= "default" namespace= "/" extends = "struts-default" >
< ! -- 动态方法调用第二种 感叹号方式 不推荐-- >
< ! -- struts2. 5 为了增加安全性,在 struts. xml 添加了这么个属性:regex: . * -- >
< global- allowed- methods> regex: . *< / global- allowed- methods>
< action name= "helloworld"
class = "com.chen.action.HelloWorldAction"
method= "execute" >
< result name= "success" > / HelloWorld. jsp< / result>
< result name= "add" > / add. jsp< / result>
< result name= "update" > / update. jsp< / result>
< / action>
< / package >
< constant name= "struts.enable.DynamicMethodInvocation" value= "true" > < / constant>
< ! -- 在浏览器打开http: / / localhost/ HelloWorld/ helloworld. action 或者在helloworld后面加上! add或者! update-- >
< / struts>
第三种方法:通配符方法 推荐
< ? xml version= "1.0" encoding= "UTF-8" ? >
< ! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd" >
< struts>
< package name= "default" namespace= "/" extends = "struts-default" >
< ! -- 动态方法调用第三种 通配符方式 推荐-- >
< ! -- struts2. 5 为了增加安全性,在 struts. xml 添加了这么个属性:regex: . * -- >
< ! -- 通配符方式一: -- >
< global- allowed- methods> regex: . *< / global- allowed- methods>
< action name= "helloworld_*"
class = "com.chen.action.HelloWorldAction"
method= "{1}" >
< ! -- 在浏览器打开http: / / localhost/ HelloWorld/ helloworld. action 或者在helloworld后面加上_add或者_update -- >
< result name= "success" > / HelloWorld. jsp< / result>
< result name= "add" > / { 1 } . jsp< / result>
< result name= "update" > / { 1 } . jsp< / result>
< / action>
< ! -- 通配符方式二:name属性中如果有多个通配符的写法:name= "helloworld_*_*" 并且method= "{ 1 } { 2 } -- >
< ! -- 通配符方式三:
< action name= "*_*"
class = "com.chen.action.{1}Action"
method= "{2}" >
http: / / localhost/ _200128StrutsHello/ HelloWorld. action 类名除去Action后的字段后面加上_add或者_update
< result name= "success" > / HelloWorld. jsp< / result>
< result name= "add" > / { 2 } . jsp< / result>
< result name= "update" > / { 2 } . jsp< / result>
< / action> -- >
< / package >
< / struts>
java文件
package com. chen. action;
import javax. servlet. ServletContext;
import javax. servlet. ServletRequest;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
import org. apache. struts2. interceptor. ServletRequestAware;
import org. apache. struts2. interceptor. ServletResponseAware;
import org. apache. struts2. util. ServletContextAware;
import com. opensymphony. xwork2. ActionSupport;
public class HelloWorldAction extends ActionSupport
implements ServletRequestAware , ServletResponseAware, ServletContextAware{
private HttpServletRequest request;
public String add ( ) {
return "add" ;
}
public String update ( ) {
return "update" ;
}
@Override
public String execute ( ) throws Exception {
System. out. println ( "执行成功" ) ;
return SUCCESS;
}
@Override
public void setServletContext ( ServletContext arg0) {
}
@Override
public void setServletResponse ( HttpServletResponse arg0) {
}
@Override
public void setServletRequest ( HttpServletRequest arg0) {
}
}