一、项目结构
二、新建UserAction,继承自ActionSupport,并实现ModelDriven<User>接口;getModel方法是用来装配对象属性的;注意,userService字段名要和applicationContext.xml中配置的userservice bean中id名称一致
package com.hjp.action; import com.hjp.domain.User; import com.hjp.service.UserService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; /** * Created by JiaPeng on 2015/12/13. */ public class UserAction extends ActionSupport implements ModelDriven<User> { User user = new User(); @Override public User getModel() { return user; } private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public String register(){ userService.register(user); return SUCCESS; } }
UserAction
三、新建index.jsp文件,代码如下
<%-- Created by IntelliJ IDEA. User: JiaPeng Date: 2015/12/13 Time: 11:36 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> <form method="post" action="${pageContext.request.contextPath}/userAction_register"> 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password"/><br/> 年龄:<input type="text" name="age"/><br/> <input type="submit" value="注册"/> </form> </body> </html>
index.jsp
四、新建struts.xml文件,及message.jsp文件
<?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.devMode" value="true"></constant> <!--包--> <package name="hello" namespace="/" extends="struts-default"> <action name="userAction_*" class="com.hjp.action.UserAction" method="{1}"> <result name="success">/message.jsp</result> </action> </package> </struts>
struts.xml
<%-- Created by IntelliJ IDEA. User: JiaPeng Date: 2015/12/13 Time: 16:23 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> 成功! </body> </html>
message.jsp
五、配置web.xml文件,主要是配置spring和struts
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--spring--> <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> <!--struts--> <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>
web.xml
时间: 2024-10-12 03:25:22