11、整合SSH框架之添加一个成员
1、我们写一个天机成员的jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>SSH之人物添加列表</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:form action="Action_add" method="post"> 名字:<s:textfield name="name" /> <input type="submit" value="提交" /> </s:form> </body> </html>
2、修改Struts.xml文件
<?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="struts2.ObjectFactory" value="Spring" /> --> <package name="person" namespace="/page" extends="struts-default"> <action name="Action_*" class="PersonAction" method="{1}"> <result name="list">/page/personlist.jsp</result> <result name="add">/page/addperson.jsp</result> </action> </package> </struts>
3、修改action
/** * 功能:集成ssh框架 * author:cutter_point * 时间:2015年3月29日17:30:07 */ package cn.cutter_point.web.action; import javax.annotation.Resource; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import cn.cutter_point.bean.Person; import cn.cutter_point.service.PersonService; import com.opensymphony.xwork2.ActionSupport; public class PersonAction extends ActionSupport { @Resource private PersonService personService; //先按名字注入,如果找不到的话就按类型注入 private String name; //名字 public String getName() { return name; } public void setName(String name) { this.name = name; } public String add() throws Exception { personService.save(new Person(name)); return "add"; } public String list() throws Exception { /* //获取实例,方法1 ServletContext sc = ServletActionContext.getRequest().getSession().getServletContext(); WebApplicationContext wac = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); //方法2 WebApplicationContext webApplicationContext = WebApplicationContextUtils. getRequiredWebApplicationContext(ServletActionContext.getServletContext()); if(wac == webApplicationContext) { System.out.println("ok!!!"); } PersonService personService = (PersonService) wac.getBean("personServiceBean");*/ HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("persons", personService.getPersons()); return "list"; } }
4、好的,我们接下来看看效果
开始的时候数据库中
我们插入一个数据
我们点击提交
结果显示出来了!!!!!
好吧,那就结束吧,恩!你没看错,就是结束了,为什么??因为没有出现乱码!!!
我是想出现中文乱码问题那就解决一下,看看如何解决的,但是它不出现,我们不能强迫他,恩,结束了!!!!
时间: 2024-10-25 12:47:27