81,092
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>DatabaseController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DatabaseController</servlet-name>
<url-pattern>/studentsmanager/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!--处理从页面传递中文到后台而出现的中文乱码问题-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.tansun.spds" />
<context:annotation-config />
<mvc:annotation-driven />
<!-- ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
@Controller
@RequestMapping(value = "/studentsmanager")
public class DatabaseController {
@RequestMapping(value = "/insert")
public ModelAndView insertPage(HttpServletRequest request, HttpServletResponse response){
Student student = getAttrFromPage(request);//忽略函数实现
ModelAndView mav = new ModelAndView("consequencePage");
boolean success = false;
try{
service.initConnection();
if(service.insert(student)){
success = true;
}
else;
service.closeConnection();
}
catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
setAttrToPage(mav, new Student());
mav.addObject("consequence", "失败");
return mav;
}
if(success){
setAttrToPage(mav, student);
mav.addObject("consequence", "成功");
return mav;
}
else{
setAttrToPage(mav, new Student());
mav.addObject("consequence", "失败(数据库操作失败)");
return mav;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>增加学生数据</title>
</head>
<body>
<h3>增加学生数据</h3>
<form action="./studentsmanager/insert" method="post">
<label for="name">姓名</label>
<input name="name" type="text" value="请输入姓名"/>
<br/>
<label for="name">性别</label>
<input name="sex" type="text" value="请输入性别"/>
<br/>
<label for="name">学校</label>
<input name="school" type="text" value="请输入学校"/>
<br/>
<label for="name">年龄</label>
<input name="age" type="text" value="请输入年龄"/>
<br/>
<label for="name">年级</label>
<input name="grade" type="text" value="请输入年级"/>
<br/>
<label for="name">学号</label>
<input name="identifier" type="text" value="请输入学号"/>
<br/>
<label for="name">自我介绍</label>
<input name="selfIntroduction" type="text" value="请输入自我介绍"/>
<br/>
<input value="提交" type="submit"/>
</form>
<a href="index.html">返回主页</a>
</body>
</html>