JSP中单选按钮回显学生性别

 一个实例:根据学号把学生信息回显到JSP页面中

数据库表studentInfo设计如下:

表中的数据如下:

 

项目的结构如下:

实体类如下:

package com.icss.entity;

public class StudentInfo {
	private String studentId;
	private String studentName;
	private char studentSex;
	private int studentAge;
	public String getStudentId() {
		return studentId;
	}
	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	public char getStudentSex() {
		return studentSex;
	}
	public void setStudentSex(char studentSex) {
		this.studentSex = studentSex;
	}
	public int getStudentAge() {
		return studentAge;
	}
	public void setStudentAge(int studentAge) {
		this.studentAge = studentAge;
	}
}

dao层接口如下:

package com.icss.dao;

import org.apache.ibatis.annotations.Select;

import com.icss.entity.StudentInfo;

public interface IStudentInfoDao {
	@Select("select *from studentInfo where studentId=#{0}")
	public StudentInfo showStudentInfoById(String studentId);
}

控制器Controller如下 :

package com.icss.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.icss.dao.IStudentInfoDao;
import com.icss.entity.StudentInfo;

@Controller
public class StudentController {
	@Autowired
	private IStudentInfoDao sDao;
	
	@RequestMapping("showInfoById")
	public String fun(HttpServletRequest request) {
		StudentInfo sInfo=sDao.showStudentInfoById("s0001");
		request.setAttribute("sInfo", sInfo);
		return "studentInfo";
	}
}

JSP文件如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="" method="post">
  <label>姓名</label><input type="text" name="studentName" value="${sInfo.studentName}"><br>
  <label>性别</label>
  <c:choose>
    <c:when test="${sInfo.studentSex=='M'}">
      <input type="radio" name="sex" checked="checked" value="男">男
      <input type="radio" name="sex" value="女">女
    </c:when>
    <c:otherwise>
      <input type="radio" name="sex" value="男">男
      <input type="radio" name="sex" checked="checked" value="女">女
    </c:otherwise>
  </c:choose>
  <br>
  <label>年龄</label><input type="text" name="studentAge" value="${sInfo.studentAge}"><br>
</form>
</body>
</html>

 程序运行时会出现如下错误:

原因是char类型数据在内存中是以ACSII码的形式存储

所以JSP中的判断男女修改如下:

<label>性别</label>
  <c:choose>
    <c:when test="${sInfo.studentSex==77}">
      <input type="radio" name="sex" checked="checked" value="男">男
      <input type="radio" name="sex" value="女">女
    </c:when>
    <c:otherwise>
      <input type="radio" name="sex" value="男">男
      <input type="radio" name="sex" checked="checked" value="女">女
    </c:otherwise>
  </c:choose>

运行结果如下:

 

其他文件如下:

 DbConfig.java文件

package com.icss.main;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.mchange.v2.c3p0.ComboPooledDataSource;


@Configuration
@MapperScan(basePackages= {"com.icss.dao"})
public class DbConfig {
	@Bean
	public DataSource dataSource() throws Exception {
		ComboPooledDataSource pool=new ComboPooledDataSource();
		pool.setJdbcUrl("jdbc:mysql://localhost:3306/student");
		pool.setUser("root");
		pool.setPassword("123456");
		pool.setDriverClass("com.mysql.jdbc.Driver");	
		pool.setMaxPoolSize(50);
		pool.setMinPoolSize(10);
		return pool;
	}
	@Bean
	public SqlSessionFactory sessionFactory() throws Exception {
		SqlSessionFactoryBean factoryBean=new SqlSessionFactoryBean();
		factoryBean.setDataSource(this.dataSource());
		return factoryBean.getObject();
	}
	@Bean
	public PlatformTransactionManager transactionManager() throws Exception {
		DataSourceTransactionManager txManager=new DataSourceTransactionManager(this.dataSource());
		return txManager;
	}
}

MainApplication.java

package com.icss.main;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class MainApplication implements WebApplicationInitializer{

	@Override
	public void onStartup(ServletContext context) throws ServletException {
		// TODO Auto-generated method stub
		AnnotationConfigWebApplicationContext act=new AnnotationConfigWebApplicationContext();
		act.setServletContext(context);
		act.register(MainConfig.class,DbConfig.class);
		Dynamic servlet = context.addServlet("spring", new DispatcherServlet(act));
		servlet.addMapping("*.do");
		servlet.setLoadOnStartup(1);
		
		javax.servlet.FilterRegistration.Dynamic filter = context.addFilter("utf-8", new MyFilter());
		filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
	}

}

MainConfig.java

package com.icss.main;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@EnableWebMvc//<mvc:conponent
@ComponentScan(basePackages= {"com.icss.controller","com.icss.service"})
public class MainConfig {

	@Bean
	public UrlBasedViewResolver viewResolver() {
		UrlBasedViewResolver rsl=new UrlBasedViewResolver();
		rsl.setViewClass(JstlView.class);
		rsl.setPrefix("/");
		rsl.setSuffix(".jsp");
		return rsl;
	}
	@Bean(name="multipartResolver")
	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver mprsl=new CommonsMultipartResolver();
		mprsl.setMaxUploadSize(1024L*1024L*10L);
		return mprsl;
	}
}

MyFilter.java

package com.icss.main;

import org.springframework.web.filter.CharacterEncodingFilter;

public class MyFilter extends CharacterEncodingFilter {
public MyFilter() {
	this.setEncoding("utf-8");
	this.setForceEncoding(true);
}
}

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值