1.通过maven加载依赖


    org.apache.velocity
    velocity
    1.7


    org.apache.velocity
    velocity-tools
    2.0

2.编辑web.xml



    velocitylayout
    org.apache.velocity.tools.view.servlet.VelocityViewServlet


    velocitylayout
    *.vm

3.在struts.xml中添加测试使用的action


    
        /WEB-INF/vm/example/{1}.vm
    

4.在/WEB_INF/vm下创建login.vm


    
        Velocity
        
    
    
        

你好: $username

5.创建ExampleAction

@Service("exampleAction")
public class ExampleAction extends BaseAction {
    @Autowired
    private ExampleService exampleService;
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String login() throws Exception {
        System.out.println("login");
        this.exampleService.save(this.getUsername());
        return Action.SUCCESS;
    }
    public String execute() throws Exception {
        System.out.println("execute");
        return Action.SUCCESS;
    }
}

6.最后在index.html中


    
        Example
        
    
    
        

Hello World!

用户登录

用户名:
密码: