架包:
< dependency >
< groupId > junit</ groupId >
< artifactId > junit</ artifactId >
< version > 4.3</ version >
< scope > test</ scope >
</ dependency >
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
< dependency >
< groupId > javax</ groupId >
< artifactId > javaee-api</ artifactId >
< version > 6.0</ version >
< scope > provided</ scope >
</ dependency >
< dependency >
< groupId > org.apache.struts</ groupId >
< artifactId > struts2-core</ artifactId >
< version > 2.3.4.1</ version >
</ dependency >
< dependency >
< groupId > org.apache.struts.xwork</ groupId >
< artifactId > xwork-core</ artifactId >
< version > 2.3.4.1</ version >
</ dependency >
HelloWorldAction:
public class HelloWorldAction implements Action {
private String name ="" ;
private String message ="" ;
public String getName() {
return name ;
}
public void setName(String name) {
this .name = name;
}
public String getMessage() {
return message ;
}
public void setMessage(String message) {
this .message = message;
}
public String execute() throws Exception {
this .setMessage("Hello," +this .getName()+"!" );
return "1" ;
}
}
struts.xml:
< struts >
< package name ="default" namespace ="/" extends ="struts-default" >
< action name ="helloWorld" class ="cn.happy.HelloWorldAction" >
< result name ="1" > 1.jsp</ result >
</ action >
</ package >
</ struts >
web.xml:
< web-app >
< display-name > Archetype Created Web Application</ 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 >
</ web-app >
页面:
<%@ page contentType =" text/html;charset=UTF-8 " language =" java " isELIgnored =" false " %>
<%@ taglib prefix =" s " uri =" /struts-tags " %>
< html >
< head >
< title > Title</ title >
</ head >
< body >
< h1 >
< s :property value ="message" ></ s :property >
</ h1 >
< hr />
< form action= "helloWorld.action" method= "post" >
请输入您的姓名:
< input name= "name" type= "text" />
< input type= "submit" value= "提交" />
</ form >
</ body >
</ html >