最近发现刚学过的知识忘得差不多了,写一写,记录一下。^_^
一、拷贝相关的jar包
二、拷贝applicationContext.xml和struts.xml文件到src文件夹下。
三、在web.xml文件中添加
- <!-- 配置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>
- <!-- 配置Spring监听器 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 用来定位spring XML文件的上下文配置 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext-*.xml,classpath:applicationContext-*.xml</param-value>
- </context-param>
四、编写LoginAction.java类
- package onlyfun.gray.action;
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction extends ActionSupport {
- 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;
- }
- @Override
- public String execute() throws Exception {
- // TODO Auto-generated method stub
- if(username.equalsIgnoreCase("gray") && password.equalsIgnoreCase("gray")) {
- return SUCCESS;
- }
- return INPUT;
- }
- }
五、在applicationContext.xml中添加
- <bean id="loginAction" class="onlyfun.gray.action.LoginAction"></bean>
六、在struts.xml中添加
- <!-- 配置struts.objectFactory属性值 -->
- <constant name="struts.objectFactory" value="spring"></constant>
- 和
- <action name="loginAction" class="loginAction">
- <result name="success">/success.jsp</result>
- <result name="input">/index.jsp</result>
- </action>
七、把应用程序发布到tomcat。